UITableView is a common UI control that, in real-world development, is important for custom UITableViewCell due to the limitations of native APIs, and custom cells can be passed through code or xib.
This essay is about customizing the cell through Xib.
First, we'll show you how to create xib with GIF.
Then implement the Code section, pay attention to the implementation of the code at the same time to make the code associated with the Xib. -
Here's the code, some explanations I commented on in the code.
Viewcontroller.m
////VIEWCONTROLLER.M//simple application of cx-xib in TableView////Created by Ma C on 16/3/18.//copyright©2016 year Xubaoaichiyu. All rights reserved.//#import "ViewController.h"#import "CXTableViewCell.h"StaticNSString * identifier =@"Cxcellid";@interfaceViewcontroller () <UITableViewDataSource,UITableViewDelegate>@property (nonatomic, strong) UITableView*TableView;@end@implementationViewcontroller#pragmamark-set_and_get-(UITableView *) tableview{if(!_tableview) {_tableview= [[UITableView alloc]initwithframe:cgrectmake (0, -, Cgrectgetwidth (Self.view.frame), -) Style:uitableviewstyleplain]; _tableview.Delegate=Self ; _tableview.datasource=Self ; _tableview.rowheight= -; [_tableview registernib:[uinib Nibwithnibname:@"Tableviewcellxib"Bundle:nil] forcellreuseidentifier:identifier]; } return_tableview;}#pragmamark-life-(void) viewdidload {[Super viewdidload]; [Self.view AddSubview:self.tableView];}#pragmamark-deledate-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{return 1;}-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{Cxtableviewcell* Cell =[TableView Dequeuereusablecellwithidentifier:identifier]; Cell= [[[[Uinib Nibwithnibname:@"Tableviewcellxib"bundle:nil]instantiatewithowner:self Options:nil]lastobject]; returncell; }@end
cxtableviewcell.m
////CXTABLEVIEWCELL.M//simple application of cx-xib in TableView////Created by Ma C on 16/3/18.//copyright©2016 year Xubaoaichiyu. All rights reserved.//#import "CXTableViewCell.h"@interfaceCxtableviewcell ()//This is where you write the space and then connect the controls and code on the Xib.@property (nonatomic, weak) iboutlet UILabel *Uplabel, @property (nonatomic, weak) Iboutlet UILabel*downlable, @property (nonatomic, weak) Iboutlet Uiimageview*Cximageview;@end@implementationCxtableviewcell-(Instancetype) Initwithstyle: (Uitableviewcellstyle) style Reuseidentifier: (NSString *) reuseidentifier{ Self=[Super Initwithstyle:style Reuseidentifier:reuseidentifier]; if(self) {//do not add the control to the view//Add to Contentview is the right choice for you.[Self.contentview addsubview:self. Cximageview]; [Self.contentview AddSubview:self.upLabel]; [Self.contentview addSubview:self.downLable]; } returnSelf ;}//use Xib to customize the information on cell Xib to be updated here- (void) awakefromnib {self. Cximageview.image= [UIImage imagenamed:@"caishen.jpg"]; Self.upLabel.text=@"Congratulations, Rich ."; Self.downLable.text=@"financial resources to enter"; }- (void) setselected: (BOOL) selected animated: (bool) animated {[Super setselected:selected animated:animated];}@end
The role of xib in real-world development is not limited to this, but more features await your discovery.
IOS xib simple app on TableView (custom cell via Xib)