Proficient in ios-Adding a child view to a table cell

Source: Internet
Author: User
Tags uikit macbook

#import <UIKit/UIKit.h>@interface**color; @end
NameAndColorCellTableViewCell.h
////NAMEANDCOLORCELLTABLEVIEWCELL.M//tabel Cells////Created by Jierism on 16/7/21.//copyright©2016 year jierism. All rights reserved.//#import "NameAndColorCellTableViewCell.h"@interfaceNameandcolorcelltableviewcell ()//defining two attribute variables@property (strong,nonatomic) UILabel *Namelabel, @property (strong,nonatomic) UILabel*Colorlabel;@end@implementationNameandcolorcelltableviewcell- (void) awakefromnib {[Super awakefromnib]; //Initialization Code}- (void) setselected: (BOOL) selected animated: (bool) animated {[Super setselected:selected animated:animated]; //Configure The View for the selected state}-(Instancetype) Initwithstyle: (Uitableviewcellstyle) style Reuseidentifier: (NSString *) reuseidentifier{ Self=[Super Initwithstyle:style Reuseidentifier:reuseidentifier]; if(self) {//Initialize Code//Add a child view to a table cell//This adds four labels to each table cell.CGRect Namelabelrect = CGRectMake (0,5, -, the); UILabel*namemaker =[[UILabel alloc] initwithframe:namelabelrect]; Namemaker.textalignment= Nstextalignmentright;//Align RightNamemaker.text =@"Name:"; Namemaker.font= [Uifont boldsystemfontofsize: A];                [Self.contentview Addsubview:namemaker]; CGRect Colorlabelrect= CGRectMake (0, -, -, the); UILabel*colormaker =[[UILabel alloc] initwithframe:colorlabelrect]; Colormaker.textalignment=Nstextalignmentright; Colormaker.text=@"Color:"; Colormaker.font= [Uifont boldsystemfontofsize: A];                [Self.contentview Addsubview:colormaker]; CGRect Namevaluerect= CGRectMake ( the,5, $, the); Self.namelabel=[[UILabel alloc] initwithframe:namevaluerect];                [Self.contentview Addsubview:_namelabel]; CGRect Colorvaluerect= CGRectMake ( the, -, $, the); Self.colorlabel=[[UILabel alloc] initwithframe:colorvaluerect];            [Self.contentview Addsubview:_colorlabel]; }    returnSelf ;}//The set method for name and color is overridden, and when a new value is passed, the contents of the label are updated- (void) SetName: (NSString *) n {if(![n isequaltostring:_name]) {_name=[n copy]; Self.nameLabel.text=_name; }}- (void) SetColor: (NSString *) C {if(![C Isequaltostring:_color]) {_color=[C Copy]; Self.colorLabel.text=_color; }}@end
NAMEANDCOLORCELLTABLEVIEWCELL.M
#import <UIKit/UIKit.h>@interface viewcontroller:uiviewcontroller< uitableviewdatasource>@end
ViewController.h
////VIEWCONTROLLER.M//tabel Cells////Created by Jierism on 16/7/20.//copyright©2016 year jierism. All rights reserved.//#import "ViewController.h"#import "NameAndColorCellTableViewCell.h"StaticNSString *celltableidentifier =@"Celltableidentifier";@interfaceViewcontroller ()//defining an array and an output interface@property (copy,nonatomic) Nsarray *computers, @property (weak,nonatomic) iboutlet UITableView*TableView;@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //additional setup after loading the view, typically from a nib. //define a dictionary in an arraySelf.computers = @[@{@"Name":@"MacBook Air",@"Color":@"Sliver"},                       @{@"Name":@"MacBook Pro",@"Color":@"Sliver"},                       @{@"Name":@"IMac",@"Color":@"Sliver"},                       @{@"Name":@"Mac Mini",@"Color":@"Sliver"},                       @{@"Name":@"Mac Pro",@"Color":@"Black"},]; [Self.tableview Registerclass:[nameandcolorcelltableviewcellclass] forcellreuseidentifier:celltableidentifier];}- (void) didreceivememorywarning {[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}//DataSource Method//returns the number of rows of array elements, where the number of return cannot be greater than the number of elements, otherwise crashes-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{return[Self.computers Count];}-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{Nameandcolorcelltableviewcell*cell =[TableView dequeuereusablecellwithidentifier:celltableidentifier Forindexpath:indexpath]; Nsdictionary*rowdata =Self.computers[indexpath.row]; Cell.name= rowdata[@"Name"]; Cell.color= rowdata[@"Color"]; returncell;}@end
VIEWCONTROLLER.M

The above code is manually implemented by adding 4 labels to the table cell and displaying the relevant content, running the effect

Another way to achieve this effect is to use storyboard, which is implemented with nib. However, before with a senior communication, I learned that in the future work with the code to achieve a more view layout, because it will solve a lot of problems. Before doing all of the use of storyboard, although now feel that it will be easier to use, but to the development of large-scale app when the Who know,right? Therefore, you also need to improve the ability to implement layout with code.

Proficient in ios-Adding a child view to a table cell

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.