iOS Development-Customize cell-Group purchase list by Xib

Source: Internet
Author: User

Custom cell
1. By xib
1> be careful to set the reuse identity in the Xib cell (reuse identifier)
Steps for 2> Encapsulation
* For inheritance, re-drag TableView, then implement the data source method
* New Xib describes what the cell looks like---dealcell.xib and sets the cell's height to 80, then drag the control
* New UITableViewCell Subclass---dealcell (used to encapsulate everything inside the xib)
* Change the class of cell in Xib to Dealcell
* All child controls in the Xib are owned in Dealcell (declare properties, connect)
* Each group has a lot of data, so each group buys a model, so the new model deal, encapsulates the data (because only attributes, so inherit from NSObject), in Deal.h declaration of each cell 5 attributes (one of which is the bool type)
* Add deal model properties to Dealcell, and the cell's child controls show what data depends on the data in the model's properties
@property (Nonatomic,strong) Deal *deal;
* Import Deal.h in dealcell.m and override Setdeal: method to set properties of the cell inner child control in this method based on model data
-(void) Setdeal: (Deal *) Deal
{
_deal = deal;

1. Title
_titlelabel.text = Deal.title;

2. Number of buyers
_buylabel.text = [NSString stirngwithformat:@ "Purchase:%d", dela.buy];

3. Price
_pricelabel.text = [NSString stirngwithformat:@ "Price:%d", Dela.price];

4. Picture
_iconview.image = [UIImage ImageNamed:deal.icon];

5. Whether to display the latest
_dealnewview.hidden =! Deal.isnew;
}

* Creating a class method in Dealcell quickly returns a xib created Cell object, so creating a cell requires only calling this class method.
+ (ID) Dealcell
{
return [[NSBundle Mainbundle] loadnibnamed:@ "Dealcell" Owner:nil options:nil][0];
}
After the creation, call the set method to pass the data.

* Add a reuse identity to the cell in Xib deal
A class method ID is then created in Dealcell to return the identity, which is called directly when the identity is set.
Then create a method cellheight returns the cell's height. (So the controller does not have to change the code, it is only responsible for invoking this method.) To change directly in the method. )

* Import Deal.h and DealCell.h in the controller implementation file
Declare an array member variable in the class extension _deals
In the Viewdidload
Deal *d1 = ....

_deals = @[d1, .... ];

and implement the data source method
-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section
{
return _deals.count;
]

-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath
{
1. Cache pool out of cell
Dealcell *cell = [TableView Dequeuereusablecellwithidentifier:[dealcell ID]];
2. The cache pool is not re-created
if (cell = = nil) {
cell = [Dealcell Dealcell];
}
3. Transfer model
Cell.deal = _deals[indexpath.row];

return cell;
}

3> using the cell
* Set the height of each row of cells
* Use the reuse identity to go to the cache pool to get the cell
* If the cache pool does not have a cell, create a cell
* Transfer model to cell

iOS Development-Customize cell-Group purchase list by Xib

Related Article

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.