IOS cainiao learning-using nib files to customize Cells

Source: Internet
Author: User

First, the class for creating a custom cell must inherit from UITableViewCell. We call it the CustomCell class.
Then, create a new view file. We call this file CustomCell. xib. Open CustomCell. xib, delete the view, and add a UITableViewCell. Click cell, change the cell class from UITableViewCell to CustomCell, and change identifier to CustomCell. In this way, you can define controls, define them in CustomCell, and connect them. Then, click the File's Owner File and change the Custom Class to the UIViewController that you want to use the cell. We call it ViewController.
Open the ViewController. h file and create two Property variables.
[Plain]
@ Property (strong, nonatomic) UINib * cellNib;
@ Property (strong, nonatomic) IBOutlet CustomCell * customCell;
And @ synthesize in the ViewController. m file,
[Plain]
@ Synthesize customCell;
@ Synthesize cellNib;
Return to the CustomCell. xib File, right-click the File's Owner, and connect customCell to the cell created earlier.
Finally, open the ViewController. m file and add the following code in the viewDidLoad method.
[Plain]
Self. cellNib = [UINib nibWithNibName: @ "CustomCell" bundle: nil];
Create your cell in tableView: cellForRowAtIndexPath of UITableView. The Code is as follows:
 
[Plain]
Static NSString * CellIdentifier = @ "CustomCell ";
CustomCell * cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];
If (cell = nil ){
[Self. cellNibinstantiateWithOwner: selfoptions: nil];
Cell = customCell;
Self. customCell = nil;
}
Then, you can set the properties and content of the control you have added to the cell.
In this way, the custom cell is implemented from the nib file.
If any omissions exist, please note.
[Plain]
<Pre>

 

 

From soloterry's column

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.