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