UITableViewCell: Custom cells, you can create cells in xib, or you can create cells in Storyborad. There are four ways to create
<1> cells created in Storyborad, which are static cells, are available at the beginning of the cell and can be loaded directly according to the custom reuse identity name;<2> of course, cells in Storyborad can also be associated with a custom class, which must be inherited UITableViewCell, which can be loaded directly based on the custom reuse identity name. <3> cells created in Xib can be loaded directly from the reuse identifier if they are loaded directly through the Bundel Loadnibnme method;<4> Of course, the cells in the Xib file can be associated with a custom class that must be inherited UITableViewCell, in which case it is not feasible to load using the custom reuse identifier directly, Because the proxy method does not initialize the Cell object at this time, it is necessary to encapsulate the process of creating the Cell object into its own associated class, that is, a class method of the created cell is used to load the Xib file, an instance method of a class object that is used to set the properties in the cell. This is an example of a contact table, with names and images, which can be achieved in the following four ways:method One: Create a cell directly in storyboard and load it directly, and customize the cell position on top of a uitableviewYou need to set the reuse identifier for a cell identifier: The code looks like this: A class created for initializing data:
1 #import <foundation/foundation.h>2 3 @interface contact:nsobject4 @property (copy,nonatomic) nsstring *name;5 @p Roperty (copy,nonatomic) nsstring *facename;6-(Instancetype) Initwithname: (nsstring*) name Andfacename: (NSString*) Facename;7 @end
1 #import "Contact.h" 2 3 @implementation Contact 4-(Instancetype) Initwithname: (nsstring*) name Andfacename: ( nsstring*) FaceName 5 {6 Self = [Super init], 7 if (self) 8 {9 _name = [name copy];10 _facename = [FA Cename copy];11 }12 return self;13}14 @end
To complete the code in the View controller: (You need to get the property control of the cell with tag)
1 #import "ViewController.h" 2 #import "Contact.h" 3 @interface Viewcontroller () <UITableViewDataSource> 4 @propert Y (weak, nonatomic) Iboutlet UITableView *tableview; 5 @property (strong,nonatomic) Nsmutablearray *contacts; 6 @end 7 8 @implementation Viewcontroller 9-(void) Viewdidload {One-to-one [super VIEWDIDLOAD];12//Initialize data-Self. contacts = [Nsmutablearray arraywithcapacity:9];14 for (int i=0; i<9; i++) {+ Contact *CONATCT = [[Co Ntact alloc]initwithname:[nsstring stringwithformat:@ "name%d", i+1] andfacename:[nsstring stringWithFormat:@ "%d.png ", i]];17 [self.contacts addobject:conatct];18}19 20//Set TableView data source Self.tableView.dataSource = self;22}23 #pragma mark-tableview Data source method 25//number of lines per group-(Nsinteger) TableView: (UITableView *) TableView NUMBEROFROWSI Nsection: (Nsinteger) section27 {self.contacts.count;29}30//set contents of each cell to-(UITableViewCell *) TableView: (UI TableView *) TableView Cellforrowatindexpath: (NSIndexpath *) IndexPath32 {33//1. According to Reuseidentifier, first go to the object pool to find the reusable cell object. static NSString *reuseidentifier = @ "MyCell"; UITableViewCell *cell = [Self.tableview dequeuereusablecellwithidentifier:reuseidentifier];36//2. Set the contents of a Cell object 37 Contact *contact = [self.contacts objectatindex:indexpath.row];38 UILabel *label = (uilabel*) [cell viewwithtag:1]; Label.text = contact.name;40 Uiimageview *imageview = (uiimageview*) [Cell viewwithtag:2];41 [ImageView SetI Mage:[uiimage imagenamed:contact.facename]];42 return cell;43}44 @end
Method Two: Create a cell directly in storyboard and associate the custom class and load it directly, customizing the cell position on top of a UITableView
You need to set the reuse identifier for a cell identifier The associated code for the cell with the corresponding custom class: A class created for initialization:
#import <Foundation/Foundation.h> @interface contact:nsobject@property (copy,nonatomic) NSString *name;@ Property (copy,nonatomic) NSString *facename;-(instancetype) Initwithname: (nsstring*) name Andfacename: (NSString*) faceName; @end #import "Contact.h" @implementation contact-(Instancetype) Initwithname: (nsstring*) name Andfacename: ( nsstring*) facename{Self = [super init]; if (self) { _name = [name copy]; _facename = [faceName copy]; } return self;} @end
The custom class associated with the cell, the property control of the associated cell (no need to use tag to get it, directly with self.)
or finish loading in the View controller:
1 #import "ViewController.h" 2 #import "Contact.h" 3 #import "myTableViewCell.h" 4 @interface viewcontroller () <uitabl Eviewdatasource> 5 @property (weak, nonatomic) Iboutlet UITableView *tableview; 6 @property (strong,nonatomic) Nsmutablearray *contacts; 7 @end 8 9 @implementation ViewController10-(void) viewdidload {[Super VIEWDIDLOAD];13//Initialize data-Self. contacts = [Nsmutablearray arraywithcapacity:9];15 for (int i=0; i<9; i++) + + Contact *CONATCT = [[Co Ntact alloc]initwithname:[nsstring stringwithformat:@ "name%d", i+1] andfacename:[nsstring stringWithFormat:@ "%d.png ", i]];18 [self.contacts addobject:conatct];19}20 21//Set TableView data source Self.tableView.dataSource = self;23}24 #pragma mark-tableview data source method 26//number of lines per group-(Nsinteger) TableView: (UITableView *) TableView NUMBEROFROWSI Nsection: (Nsinteger) section28 {return self.contacts.count;30}31//set contents of each cell up to-(UITableViewCell *) TableView: (UI TableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexPath33 {34//1. According to the Reuseidentifier, first go to the object pool to find the reusable cell object. Static NSString *reuseidentifier = @ "MyCell"; Mytableviewcell *cell = [Self.tableview dequeuereusablecellwithidentifier:reuseidenti fier];37//2. Setting the contents of a Cell object. Contact *contact = [self.contacts objectatindex:indexpath.row];39 cell.label.text = contact.name;40 [Cell.imgview setimage:[uiimage imagenamed:contact.facename]];41 return cell;42}43 @end
Method Three: Create a cell in the Xib file and then load it directly in the view controller using the
First, add a uitableview to the storyboard
Then create a custom cell in the mycell.xib that has been created:
Set the reuse identifier for this cell identifier:
Create a contact-initialized class:
1 #import <Foundation/Foundation.h> 2 3 @interface contact:nsobject 4 @property (copy,nonatomic) NSString *nam E 5 @property (copy,nonatomic) NSString *facename; 6-(Instancetype) Initwithname: (nsstring*) name Andfacename: (nsstring*) FaceName; 7 @end 8 9 #import "Contact.h" @implementation Contact13-(Instancetype) Initwithname: (nsstring*) name Andfacename: (nsstring*) faceName14 {self = [Super init];16 if (self) + { _name = [name copy];19 _facename = [FaceName copy];20 } return self;22}23 @end
or finish loading in the View controller:
1 #import "ViewController.h" 2 #import "Contact.h" 3 #import "myTableViewCell.h" 4 @interface viewcontroller () <uitabl Eviewdatasource> 5 @property (weak, nonatomic) Iboutlet UITableView *tableview; 6 @property (strong,nonatomic) Nsmutablearray *contacts; 7 @end 8 9 @implementation ViewController10-(void) viewdidload {[Super VIEWDIDLOAD];13//Initialize data-Self. contacts = [Nsmutablearray arraywithcapacity:9];15 for (int i=0; i<9; i++) + + Contact *CONATCT = [[Co Ntact alloc]initwithname:[nsstring stringwithformat:@ "name%d", i+1] andfacename:[nsstring stringWithFormat:@ "%d.png ", i]];18 [self.contacts addobject:conatct];19}20 21//Set TableView data source Self.tableView.dataSource = self;23}24 #pragma mark-tableview data source method 26//number of lines per group-(Nsinteger) TableView: (UITableView *) TableView NUMBEROFROWSI Nsection: (Nsinteger) section28 {35 return self.contacts.count;30}31 32 33//load directly from the Xib file 34///Set the contents of each cell (uitable Viewcell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexPath37 {38//1. According to Reuseidentifier, First go to the object pool to find the reusable cell object. nsstring *reuseidentifier = @ "MyCell"; *cell Self.tableview uereusablecellwithidentifier:reuseidentifier];41//2. If not found, create yourself cell42 if (!cell) 43 {44//Load view from Xib file 45 Nsarray *views = [[NSBundle mainbundle]loadnibnamed:@ "MyCell" owner:nil options:nil];46 cell = (UITableVie wcell*) [Views lastobject];47}48//3. Setting the contents of a Cell object. Contact *contact = [Self.contacts objectAtIndex:indexPath.ro w];50 UILabel *label = (uilabel*) [cell viewwithtag:1];51 label.text = contact.name;52 Uiimageview *imgview = ( uiimageview*) [Cell viewwithtag:2];53 [Imgview setimage:[uiimage imagenamed:contact.facename]];54 return cell ; 56}
Method Four: Create a cell in the Xib file, create a class associated with it, and then encapsulate the loading process into its class to help initialize the completion, while the class provides the class method, and finally the view controller gets the cell through this class method.
First, add a uitableview to the storyboard
Then create a custom cell in the mycell.xib that has been created:
Set the reuse identifier for a cell identifier
Associating a cell with a custom class
Create a contact-initialized class:
1#import <Foundation/Foundation.h> 2 3 @interface contact:nsobject 4 @property (copy,nonatomic) NSString * Name 5 @property (copy,nonatomic) NSString *facename; 6-(Instancetype) Initwithname: (nsstring*) name Andfacename: (nsstring*) FaceName; 7 @end 8 9 #import "Contact.h" @implementation Contact13-(Instancetype) Initwithname: (nsstring*) name Andfacename: (nsstring*) faceName14 {self = [Super init];16 if (self) + { _name = [name copy];19 _facename = [FaceName copy];20 } return self;22}23 @end
Create a class associated with a cell: (Encapsulates the process and properties of the loaded cell)
Get cells from the above class method in the View controller
1 #import "ViewController.h" 2 #import "Contact.h" 3 #import "myTableViewCell.h" 4 @interface viewcontroller () <uitabl Eviewdatasource> 5 @property (weak, nonatomic) Iboutlet UITableView *tableview; 6 @property (strong,nonatomic) Nsmutablearray *contacts; 7 @end 8 9 @implementation ViewController10-(void) viewdidload {[Super VIEWDIDLOAD];13//Initialize data-Self. contacts = [Nsmutablearray arraywithcapacity:9];15 for (int i=0; i<9; i++) + + Contact *CONATCT = [[Co Ntact alloc]initwithname:[nsstring stringwithformat:@ "name%d", i+1] andfacename:[nsstring stringWithFormat:@ "%d.png ", i]];18 [self.contacts addobject:conatct];19}20 21//Set TableView data source Self.tableView.dataSource = self;23}24 #pragma mark-tableview data source method 26//number of lines per group-(Nsinteger) TableView: (UITableView *) TableView NUMBEROFROWSI Nsection: (Nsinteger) section28 {return self.contacts.count;30}31//load Xib file in the class associated with xib (in fact it is encapsulated) 32 33//set each cell's Content-(UitablevieWcell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexPath35 {36//1. According to Reuseidentifier, first go to the object pool to find the reusable Cell object NSString *reuseidentifier = @ "MyCell"; Mytableviewcell *cell = [self . TableView dequeuereusablecellwithidentifier:reuseidentifier];39//2. If you don't find it, create it yourself cell40 if (!cell) 41 {42 cell = [Mytableviewcell cell];//calls class method 43}44//3. Set the contents of a Cell object. *contact = [Self.contacts objectatindex: indexpath.row];46 [Cell setcontact:contact];//Invoke instance method cell;49 return}50 Wuyi @end
Ios:uitableviewcell a custom cell