Common iOS controls-UITableViewCell and-uitableviewcell

Source: Internet
Author: User

Common iOS controls-UITableViewCell and-uitableviewcell

1. encapsulation cell:

1. Loading xib files in two ways

<Method 1> (NewsCell is the name of the xib file)
NSArray * objects = [[NSBundle mainBundle] loadNibNamed: @ "NewsCell" owner: nil options: nil];

<Method 2>
UINib * nib = [UINib nibWithNibNamed: @ "NewsCell" bundle: nil];
NSArry * objects = [nib instantiateWithOwner: nil options: nil];

 

2. Set the cell height

1. If the cell height of each row is the same, use the proxy method to set the cell height.
-(CGFloat) tableView :( UITableView *) tableView heightForRowAtIndexPath :( NSIndexPath *) indexPath

{

// If the height of each row is the same, you can set self. tableView. rowHeight = 80 in viewDidLoad;
// Returns the height of the cell corresponding to the row indexPath.
Return 80;
}

 

========================================================== ======================================

3. Custom cell: method 1. Describe cell through xib: 1> be sure to set reuse identifier in the cell of xib. 2> encapsulation steps
Create xib to describe the appearance of the cell -- DealCell. xib creates a subclass of UITableViewCell (defines a class DealCell and inherits from UITableViewCell). -- DealCell (encapsulates everything in xib) modifies the cell class name (class) in xib to DealCell. In DealCell, all the child controls in xib are available (declaring properties and connecting lines ). Create a new model Deal (defines a class Deal that inherits from NSObject) and encapsulate data. Add model attributes to DealCell (you can get the data obtained from the server to the xib control from Deal to display it)
In DealCell: @ class Deal; @ interface DealCell: UITableViewCell @ property (nonatomic, weak) IBOutlet UIImageView * iconView; @ property (nonatomic, weak) IBOutlet UIImageView * dealNewView; @ property (nonatomic, weak) IBOutlet UILabel * titleLabel; @ property (nonatomic, weak) IBOutlet UILabel * buyLabel; @ property (nonatomic, weak) IBOutlet UILabel * priceLabel; @ property (nonatomic, strong) Deal * deal; + (id) dealCell; + (NSString *) ID; # end
In DealCell, override the setDeal method (in this method, set the attributes of the Child control in the cell based on the model data): # import "DealCell. h" # import "Deal. h" @ implementation DealCell
-(Void) setDeal :( Deal *) deal {_ deal = deal; // 1. title _ titleLabel. text = deal. title; // 2. number of purchasers _ buyLabel. text = [NSString stringWithFormat: @ "Purchase: % d", deal. buy]; // 3. price _ priceLabel. text = [NSString stringWithFormat: @ "Price: % f", deal. price]; // 4. avatar _ iconView. image = [UIImage imageNamed: deal. icon]; // 5. whether to display the latest _ newView. hidden =! Deal. isNew ;}
+ () DealCell {return [[NSBundle mainBundle] loadNibNamed: @ "DealCell" owner: nil options: nil] [0];}
+ (NSString *) ID {return @ "deal";} @ end
Provides a class method to return the cell object created from xib. Add a reuse identifier (such as deal) to the cell in xib, and then provide a class method to return a reuse identifier. Provides a class method to return the height of the cell. 3> use cell to set the height of each cell row. Use the reuse ID to get the cell from the cache pool. If there is no cell in the cache pool, create the cell and pass the model to the cell (this step is in-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) in the indexPath method) method 2. Use the code to customize the cell:
1> Create a subclass of UITableViewCell-weiboCell. 2> Add the child control to be used inside the cell when creating the cell (initWithStyle: reuseIdentifier: method. 3> create a model class, Weibo, and add corresponding data attributes. 4> Add a Weibo model attribute to WeiboCell and set the attributes of the subcontrol while obtaining the Weibo model data. 5> rewrite setWeibo: The method where the data of the Weibo model is retrieved and displayed on the subcontrol.
In Deal: @ interface Deal: NSObject @ property (nonatomic, copy) NSString * icon; @ property (nonatomic, copy) NSString * title; @ property (nonatomic, assign) int buy; @ property (nonatomic, assign) double price; @ property (nonatomic, assign) BOOL isNew; # end

 

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.