Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I have created custom TableViewCells before, you can create a custom class just like you would for viewControllers (File > New > File). When choosing which object to subclass, choose UITableViewCell. </p> <p>Unfortunately, Xcode will not give you the option to include a .xib file, so you'll have to create one of those too: File > New > File, but this time on the left side of the window that pops up, select 'User Interface' instead of 'Cocoa Touch'. Choose the 'Empty' icon at this screen.</p> <p>You will be asked whether the device family is iPhone or iPad. Since you are making a table view cell not a viewController, it doesn't matter which you pick.</p> <p>When you create this object you will be presented with a blank canvas. Drag a 'Table View Cell' from the object library to the canvas and you're set to start creating!</p> <p>It is of note that where you'd normally create outlets, you should create properties for TableViewCells. This is because other objects are going to be interacting with the TableViewCell, not just the TableViewCell's implementation file.</p> <p>Here is what a header file for a custom TableViewCell might look like:</p> <pre><code>@interface MyCustomCell : UITableViewCell @property (weak, nonatomic) IBOutlet UIImageView *icon; @property (weak, nonatomic) IBOutlet UILabel *label; @property (weak, nonatomic) IBOutlet UILabel *number; @end </code></pre> <p>My implementation file was empty.</p> <p>However, that's not all. There is also some work to be done in the TableViewController that will utilize this cell. Be sure to import your TableViewCell's header file.</p> <p>First, the cell has to be registered in the viewDidLoad method</p> <pre><code>// Resiter the Nib UINib *nib = [UINib nibWithNibName:@"MyCustomCell" bundle:nil]; [[self tableView] registerNib:nib forCellReuseIdentifier:@"MyCustomCell"]; </code></pre> <p>And then when the TableViewController determines how to draw the cells:</p> <pre><code>- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCustomCell"] // Then here you can set the properties of each cell [[cell label] setText:@"Some text here"]; [[cell number] setText:[NSString stringWithFormat:@"%d", 5]]; [[cell icon] setImage:[UIImage imageNamed:@"myImage]]; } </code></pre> <p>I hope this is helpful!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload