Creating custom UITableViewCell's within a Storyboard

32,935

Solution 1

If you have set your UITableView to be using 'Static Cells' in the storyboard, you don't need to implement any of the UITableViewDataSource methods and you can modify the cell directly in Interface Builder. For a single label cell, select the cell and change it's type to 'Basic'. You can now edit the cell just like you would any other view object.

Solution 2

Here is what I've learned about how you get cells for your table when using the storyboard. When you drag a UITableView into your view, it comes with a prototype cell already set as a subview. To use this prototype cell, set a unique reuse identifier in the attributes inspector, and then use the same identifier to dequeue the cell in your cellForRowAtIndexPath: method. I leave out the code for creating a cell from scratch if the dequeue call returns nil; I don't think it can happen. So just dequeue the cell, configure it with the usual UITableViewCell methods, and return it.

But you can also create custom subclasses of UITableViewCell. Just set the class name in the storyboard's class identity inspector, and drag whatever elements you want from the Objects palette into your cell. Then create IBOutlet properties for them in your subclass's code files, and hook them up to the cell in the storyboard in the usual way. This is so much better than having to do it all in code!

And finally, you can have more than one kind of cell in your table. Just drag UITableViewCell objects from the palette into the table, and give each one a unique reuse identifier in the attributes inspector. In your cellForRowAtIndexPath: method, choose the type of each cell and you can have a very flexible table view.

Solution 3

This tutorial was helpful to me. You can reference whatever object you need through the tag.

In the Storyboard drag on a UIImageView or UILabel etc. and set the tag to 100 (whatever you want) then in your - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath use the tag to reference it.

Here is the example code in the tutorial, just remember to set the tags in the storyboard:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

// Configure the cell...
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

// Display recipe in the table cell
Recipe *recipe = [recipes objectAtIndex:indexPath.row];
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:recipe.imageFile];

UILabel *recipeNameLabel = (UILabel *)[cell viewWithTag:101];
recipeNameLabel.text = recipe.name;

UILabel *recipeDetailLabel = (UILabel *)[cell viewWithTag:102];
recipeDetailLabel.text = recipe.detail;

return cell;
 }
Share:
32,935
Doug
Author by

Doug

Updated on July 09, 2022

Comments

  • Doug
    Doug almost 2 years

    Wanting to create a static menu (IOS 5) and attempting to create custom cells within the storyboard to then load onto the grouped tableview.

    I've created the outlet

    @property(nonatomic,strong) IBOutlet UITableViewCell *labelCell;
    

    The ViewController class is set to the proper TableViewController and I've connected the custom cell to this outlet.

    I also have the delegate and datasource set up.

    I've got

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath   *)indexPath
    {
      return self.labelCell;
    }
    

    I'm sure there is a ton wrong with this, but I'm just trying to display one cell and go from there. There does not seem to be any examples of doing custom cells within the IB through the storyboard. I can still use the old way of creating a xib file and loading it in the mainBundle but I just want to stay up to date I guess.

    but with what I have above i get a crash when I load this view controller. SIGABRT

  • Doug
    Doug over 12 years
    Yep. That's about it. Not sure why I did not find any guides at all that told you to do this. Makes it much easier.
  • RDM
    RDM over 12 years
    As a random question, does using static cells imply that you have exactly the amount of cells you specify in IB? Since you don't have to implement the data source methods I suppose you can't change the data in your table view and that's why they called it static - right?
  • jrturton
    jrturton over 12 years
    @Warkst - exactly. In fact, you should remove the stub implementations of the data source methods.
  • jrturton
    jrturton over 12 years
    This is all true, but the question is about static table views, which don't use prototypes.
  • thesummersign
    thesummersign almost 12 years
    I have a new scenario (doubt). If I have a UITableView with custom cell that contains +, - buttons and UITextField. all setup is done according to your paragraph #2 already but now if i want to prevent my cells to be reused is there any good way of doing it? should i even do that? (i want the functionality of adding more than one email ID)
  • Greg M. Krsak
    Greg M. Krsak over 10 years
    @jrturton Ah, but some arrive here from a Google search, without realizing the "static" part. The title is a bit misleading. So this answer actually was what I was looking for.