The importance of UITableView's development in iOS is beyond doubt, with roughly half the percentage used in the app, and most of the cases requiring custom cells, so that users can see apps that are more aesthetically pleasing. Before written UITableView basic usage, if the UITableView is not very familiar with my previous blog, so a lot of uitableview knowledge of the default you are already familiar with, first look at the effect of the custom implementation, this is the end of the custom cell display effect :
Custom Cell
1. First create a new Customcell.xib file, in the following manner:
2. Create a new Customtableviewcell that inherits from UITableViewCell class, you need to override the Initwithstyle method:
-(Instancetype) Initwithstyle: (Uitableviewcellstyle) style Reuseidentifier: (NSString *) reuseidentifier{ self=[ Super Initwithstyle:style Reuseidentifier:reuseidentifier]; if (self) { Nsarray *views = [[NSBundle mainbundle] loadnibnamed:@ "Customcell" Owner:nil Options:nil]; Self=[views firstobject]; } return self;}
Drag a UITableViewCell into the 3.customcell.xib, and then associate with Customtableviewcell:
After association, Customtableviewcell has one more attribute:
@property (Weak, nonatomic) Iboutlet UILabel *title;
UITableView Load Cell
1.viewDidLoad initialization UITableView:
Self.tableview=[[uitableview Alloc]initwithframe:cgrectmake (10,cgrectgetwidth (self.view.bounds)-20, Cgrectgetheight (Self.view.bounds) -10)]; self.tableview.delegate=self; self.tableview.datasource=self; [Self.view AddSubview:self.tableView];
2. Initialize the header array travelarr:
-(Nsarray *) travelarr{ [email protected][@ "Beijing-Chiangmai", @ "Beijing-Hong Kong", @ "Beijing-Tokyo", @ "Beijing-Osaka", @ "Beijing-Singapore", @ "BEIJING-Victoria" @ "Beijing-New York" @ "Beijing-Hawaii", @ "Beijing-Victoria", @ "Beijing-Cambodia"; return _travelarr;}
3. Implement the Methods in Uitableviewdatasource:
-(Nsinteger) Numberofsectionsintableview: (UITableView *) tableview{ return 1;} -(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{ return [ Self.travelarr count];} -(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{ static nsstring *[email protected] "Customcell"; Customtableviewcell *cell=[tableview Dequeuereusablecellwithidentifier:identifier]; if (cell==nil) { Cell=[[customtableviewcell alloc]initwithstyle:uitableviewcellstyledefault reuseIdentifier: identifier]; } Cell.title.text=[self.travelarr ObjectAtIndex:indexPath.row]; return cell;}
4. Implement the Methods in Uitableviewdelegate:
-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) indexpath{ UITableViewCell *cell = [self Tableview:tableview cellforrowatindexpath:indexpath]; return cell.frame.size.height;}
Simple customization is probably the case, if the harvest, help recommend the next ~
iOS Developer-uitableview Custom cell