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
Simple application of viewcontroller.m//cx-xib in TableView////Created by Ma C on 16/3/18.//Copyright? 2016 Xubaoaichiyu. All rights reserved.//#import "ViewController.h" #import "CXTableViewCell.h" static NSString * identifier = @ "Cxcellid"; @ Interface Viewcontroller () <UITableViewDataSource,UITableViewDelegate> @property (nonatomic, Strong) UITableView * TableView; @end @implementation viewcontroller#pragma mark-set_and_get-(UITableView *) tableView{if ( !_tableview) {_tableview = [[UITableView alloc]initwithframe:cgrectmake (0, Cgrectgetwidth (self.view.fr AME), Style:uitableviewstyleplain]; _tableview.delegate = self; _tableview.datasource = self; _tableview.rowheight = 100; [_tableview registernib:[uinib nibwithnibname:@ "Tableviewcellxib" Bundle:nil] forcellreuseidentifier:identifier]; } return _tableview;} #pragma mark-life-(void) viewdidload {[Super viewdidload]; [SELF.VIew AddSubview:self.tableView];} #pragma mark-deledate-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (Nsinteger) section{ return 1;} -(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{CXTABLEVIEWC ell * cell = [TableView dequeuereusablecellwithidentifier:identifier]; cell = [[[Uinib nibwithnibname:@ "Tableviewcellxib" bundle:nil]instantiatewithowner:self Options:nil]lastobject]; return cell; } @end
&NBSP;CXTABLEVIEWCELL.M
Simple application of cxtableviewcell.m//cx-xib in TableView////Created by Ma C on 16/3/18.//Copyright? 2016 Xubaoaichiyu. All rights reserved.//#import "CXTableViewCell.h" @interface Cxtableviewcell ()//write the space first, then connect the control and code on the Xib @property ( Nonatomic, weak) iboutlet UILabel * Uplabel; @property (nonatomic, weak) iboutlet UILabel * downlable; @property (Nonatomic, Weak) Iboutlet Uiimageview * cximageview; @end @implementation cxtableviewcell-(instancetype) Initwithstyle: ( Uitableviewcellstyle) style Reuseidentifier: (NSString *) reuseidentifier{self = [Super Initwithstyle:style ReuseIden Tifier:reuseidentifier]; if (self) {//Do not add control to view//add to Contentview is your most correct choice [Self.contentview addsubview:self. Cximageview]; [Self.contentview AddSubview:self.upLabel]; [Self.contentview addSubview:self.downLable]; } return self;} Use Xib to customize the information on cell xib to be placed here Update-(void) awakefromnib {self. Cximageview.image = [UIImage imagenamed:@ "caishen.jpg"]; Self.upLabel.text = @ "Congratulations on getting rich"; Self.downLable.text = @ "Financial resources in the broad"; }-(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)