36
votes

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;
}
5
what if you still try to use this Nib as the custom TableViewCell ?onmyway133

5 Answers

39
votes

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;
}
9
votes

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.

8
votes

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.

0
votes

I think this one works very well. I was trying to get my head around it for sometime and thanks it worked.. well i was doing this, when trying to alloc a cell i was allocating using the general cell class instead of the one i had created

so i was doing this, so thats reason why it did not work

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

so instead of UITableViewCell alloc the cell with your class, well with the above example "CustomCell"

Thanks again

0
votes
static NSString *identifier=@"SearchUser";
SearchUserCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell==nil)
{
    cell=[[SearchUserCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
return cell;