How to initialize a custom prototype style table cell in iOS 5 storyboards?
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.
Tom Kincaid
Updated on May 23, 2020Comments
-
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 about 12 yearsThat results in a cell with the default style and not the custom style from the storyboard.
-
Alejandro Rangel about 12 yearsheres and example techotopia.com/index.php/…
-
Tom Kincaid about 12 yearsThanks for the link. I got it to work. A little tricky step is control dragging from an empty space on the view.
-
Claus almost 12 yearsCan you be more precise? I still don't get it. Thanks
-
Alejandro Rangel almost 12 yearsSure, but it will be more easy to solve doubt if you give a little more info
-
mskw about 11 yearsFor iOS6 with storyboards, cell is never nil after dequeuing.
-
Nurbol almost 11 yearsThis should be the accepted answer as far as I can tell. Checking for nil cells etc is useless boilerplate when using storyboards.
-
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 over 10 yearsGiven you have the correct identifier yes. Otherwise it will crash complaining about it.
-
jackslash almost 10 yearsThis answer is outdated and now wrong. developer.apple.com/library/ios/documentation/uikit/reference/…:
-
qwerty_so almost 9 yearsSo what does that code mean? Some commentary would have helped.
-
datWooWoo almost 9 years@jackslash We're not all using storyboards.
-
jackslash almost 9 yearsNope. Now you register table cell classes with the table view in advance and when you dequeue the cell is guaranteed to exist.
-
datWooWoo almost 9 yearsOh I see. I thought you were pointing something else out. Should try to edit the answer then.