Cell's Factory mode

Source: Internet
Author: User

The cell has a table view and a collection view of cell two, and the Factory mode is implemented in roughly the same way.

1. First introduce the cell factory of the table view

1): First create the cell base class Basetableviewcell that inherits from UITableViewCell, and use the custom cell in the project to inherit from this class (if you need a controller to control more than two! different! Then the second cell needs to be written in code).

2): Declare a method of setting data in the underlying cell class, and empty implement

-(void) Setdatawithmodel: (basemodel *) model;

3): Create the Cell factory class Factorytableviewcell and define a method to create a cell from the model

+ (Basetableviewcell *) Createtableviewcellwithmodel: (Basemodel *) model TableView: (UITableView *) TableView Indexpath :(Nsindexpath *) Indexpath

{

1. Get the name of the cell

NSString *name = Nsstringfromclass ([model class]);

2. Create a cell

Basetableviewcell *cell = [TableView dequeuereusablecellwithidentifier:name Forindexpath:indexpath];

}

4): Use of the plant

①: Registering the cell

[self. Mytableview registernib: [uinib nibwithnibname:@ "Readdetailcell" Bundle:nil] forcellreuseidentifier:nsstringfromclass([ Readdetaillistmodel class]);

②: Calling methods in the factory in the method of reusing cells

-(uitableviewcell *) TableView: (uitableview *) TableView Cellforrowatindexpath: (nsindexpath *) Indexpath

{

//1. Get model

Basemodel *model = self. Alllistarray[Indexpath. Row];

Basetableviewcell *cell = [factorytableviewcell createtableviewcellwithmodel: Model TableView: TableView indexpath: Indexpath];

[Cell Setdatawithmodel: Model];

return cell;

}

Cell's Factory mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.