0

I making the tutorial on running iPhone 5S, Xcode 6 and iOS 8. I want to display the textDetailLabel in a cell table. Can you help me what's the problem here. I already checked the syntax but it doesn't work

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
Checklist *checklist = [self.dataModel.lists objectAtIndex:indexPath.row];
cell.textLabel.text = checklist.name;
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

int count = [checklist countUncheckedItems];
if ([checklist.items count] == 0) {
    cell.detailTextLabel.text = @"(No Items)";
} else if (count == 0) {
    cell.detailTextLabel.text = @"All Done!";
} else {
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%d Remaining", count];
}
return cell;

}

huynhtridung
  • 43
  • 2
  • 9

1 Answers1

2
  1. Move

    static NSString *CellIdentifier = @"Cell";

out of the method, make it public.

  1. When you create the table view write this line where you create the table (in viewDidLoad or your method where you create the table)

[dataTable registerClass:[UITableViewCell class] forCellReuseIdentifier:tableIdentifier];

  1. Remove cell condition from

    • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

method because your table view cell is not nil and you can't set the cell style.

So you have to write

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
Adela Toderici
  • 1,118
  • 13
  • 30