How to initialize a custom prototype style table cell in iOS 5 storyboards?

54,329

Solution 1

This way

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    return cell;
}

Solution 2

This worked perfect for me (Xcode 4.5.2 and iOS 6.0):

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

    if( cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    }

    UILabel *title = (UILabel*) [cell viewWithTag:1000];
    UILabel *summary = (UILabel*) [cell viewWithTag:1001];
    [title setText:[ tableMainTitle objectAtIndex:indexPath.row]];
    [summary setText:[ tableSubTitle objectAtIndex:indexPath.row]];
    return cell;
}

Important: Do not forget setting delegate and datasource.

Solution 3

If you load your view controller that contains the tableview using:

MyViewController *myViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"MyViewController"];

Then inside cellForRowAtIndexPath you just need one line:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifierFromStoryboard"];

dequeueReusableCellWithIdentifier will instantiate one cell if there is none exists.

Share:
54,329
Tom Kincaid
Author by

Tom Kincaid

Updated on May 23, 2020

Comments

  • Tom Kincaid
    Tom Kincaid almost 4 years

    I am converting over to iOS 5 and storyboards. When I have a table view with default cell style, everything works fine.

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifierFromStoryboard"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyIdentifierFromStoryboard"];
        }
        return cell;
    }
    

    I have seen examples where the "if (cell == nil)" block is removed. However if I take it out, my app crashes with the message: "UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:". This is not a problem because it works as shown above.

    My problem is that I want to use a custom style for the cell and thus cannot use initWithStyle. How do I initialize a custom cell that I have designed on the storyboard?

    The old pre-5 app had a nib and class that used something like this, but now I'm using the storyboard.

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        MyCustomTableCell *cell = (MyCustomTableCell *) [tableView dequeueReusableCellWithIdentifier:@"MyCustomIdentifier"];
        if (cell == nil) {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MyCustomTableCell" owner:self options:nil];
            cell = (MyCustomTableCell *) [nib objectAtIndex:0];
        }
        return cell;
    }
    
  • Tom Kincaid
    Tom Kincaid about 12 years
    That results in a cell with the default style and not the custom style from the storyboard.
  • Alejandro Rangel
    Alejandro Rangel about 12 years
    heres and example techotopia.com/index.php/…
  • Tom Kincaid
    Tom Kincaid about 12 years
    Thanks for the link. I got it to work. A little tricky step is control dragging from an empty space on the view.
  • Claus
    Claus almost 12 years
    Can you be more precise? I still don't get it. Thanks
  • Alejandro Rangel
    Alejandro Rangel almost 12 years
    Sure, but it will be more easy to solve doubt if you give a little more info
  • mskw
    mskw about 11 years
    For iOS6 with storyboards, cell is never nil after dequeuing.
  • Nurbol
    Nurbol almost 11 years
    This should be the accepted answer as far as I can tell. Checking for nil cells etc is useless boilerplate when using storyboards.
  • twinlakes
    twinlakes over 10 years
    @mskw so a cell will always be valid and useable? or do they have some other method of identifying invalid cells (like a constant or something)
  • mskw
    mskw over 10 years
    Given you have the correct identifier yes. Otherwise it will crash complaining about it.
  • jackslash
    jackslash almost 10 years
  • qwerty_so
    qwerty_so almost 9 years
    So what does that code mean? Some commentary would have helped.
  • datWooWoo
    datWooWoo almost 9 years
    @jackslash We're not all using storyboards.
  • jackslash
    jackslash almost 9 years
    Nope. Now you register table cell classes with the table view in advance and when you dequeue the cell is guaranteed to exist.
  • datWooWoo
    datWooWoo almost 9 years
    Oh I see. I thought you were pointing something else out. Should try to edit the answer then.