1. Introduction
In TableView, the default cell format, which is organized as follows:
< interception from official website documentation >
Finally, the default cell on the page can only be displayed as shown above, if this does not meet our interface requirements, then we can customize the cell to create.
2. Implement
Target : We need to make the following table, its cell height width and layout are the same, so it is very suitable to use xib to customize the cell.
1. After initializing the project, we store the table data in plist:
2. In the controller we load the plist data in lazy loading mode
Detailed methods can be consulted on how to customize the Plist file and read the contents of the Plist file.
3. Create a Xib file
4. Select the table View cell control to draw xib based on demand, and set properties such as aspect
5. In order to re-use the cell, you need to set the cell's label:
6. Create a table cell subclass in which we create cells in a reusable way to prevent the loss of resources and performance caused by excessive creation of new cells.
. h
#import <UIKit/UIKit.h>@class Xfgoodmodel; @interface *goodinfo; + (Instancetype) Goodcellwithtableview: (UITableView *) TableView; @end
. m
#import "XFGoodTableViewCell.h"#import "XFGoodModel.h"@interfaceXfgoodtableviewcell () @property (weak, nonatomic) Iboutlet Uiimageview*Googimageview, @property (weak, nonatomic) Iboutlet UILabel*Nameview, @property (weak, nonatomic) Iboutlet UILabel*Priceview, @property (weak, nonatomic) Iboutlet UILabel*Soldnumview;@end@implementationXfgoodtableviewcell/** * Quickly initialize cell in a reusable way * * @param tableView Current TableView * * @return New or reused cell*/+ (Instancetype) Goodcellwithtableview: (UITableView *) tableview{//A reusable lookup of cells based on an ID StaticNSString *reuseid =@"Goodcell"; Xfgoodtableviewcell*cell =[TableView Dequeuereusablecellwithidentifier:reuseid]; //we need to load the cell from the xib when there is no reusable cell. if(!cell) { //Specify the Xib file name, in Xib can contain a lot of view, here we have only one cell view, so directly get Lastobjectcell = [[[NSBundle Mainbundle] loadnibnamed:@"Xfgoodcell"Owner:nil Options:nil]lastobject]; } returncell;}/** * Set the current cell data and set the value of each space in the cell * * @param goodinfo < #goodInfo description#>*/-(void) Setgoodinfo: (Xfgoodmodel *) goodinfo{_goodinfo=Goodinfo; Self.googImageView.image=[UIImage ImageNamed:goodInfo.image]; Self.nameView.text=Goodinfo.name; Self.priceView.text= [NSString stringWithFormat:@"¥%ld", (Long) Goodinfo.price]; Self.soldNumView.text= [NSString stringWithFormat:@"%ld copies sold", (Long) Goodinfo.soldnum]; }@end
7. The class needs to be associated with the table cell in Xib and the control is wired to the code.
At this point, the cell section has been created. Next we go together to implement the data source proxy.
8. In the controller itself as the TableView data source proxy, and to implement the TableView data source Proxy method:
-(void) viewdidload { [super Viewdidload]; // Do any additional setup after loading the view. // specifying the data proxy for TableView Self.goodsTableView.dataSource = self ;}
#pragmaOverriding proxy methods//returns the total amount of data-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{//1. Load xib get cell definitionXfgoodtableviewcell *cell =[Xfgoodtableviewcell Goodcellwithtableview:tableview]; //2. Setting the cell dataCell.goodinfo =Self.goodslist[indexpath.row]; //3. Return to cell returncell; }//Load Cell-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{returnSelf.goodsList.count;}
Above.
[How to] uses a custom cell for tableview creation, which is suitable for cases where the cell style does not change.