I am converting over to iOS 5 and storyboards. When I have a table view with default cell style, everything works fine.
- (UITableViewCell *)tableView:(UITab
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.
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;
}
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
static NSString *identifier=@"SearchUser";
SearchUserCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell==nil)
{
cell=[[SearchUserCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
return cell;