How to initialize a custom prototype style table cell in iOS 5 storyboards?
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;}
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.
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.