Search code examples
objective-cuitableviewnulldetailtextlabel

detailtextlabel set to nil


I have UITableViewCell cells with style = UITableViewCellStyleValue1 (this style is used in all other view controllers that I use in my project). I've double checked the cell identifiers in my storyboard as per suggestions from other SO posts. Still I get detailTextLabel as nil. Is there something else I am doing wrong ? I observed that the cells are always reused i.e if statement for allocation is never executed. So, how do I get my UITableViewCell object's style to verify that the reused cells follow the same style ?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath     *)indexPath
{
    static NSString *CellIdentifier = @"CellForDevice";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1  reuseIdentifier:CellIdentifier];
     }
     NSLog(@"reuse ID : %@",cell.reuseIdentifier);
     // Configure the cell...
     NSArray* row = [self.listOfItems objectAtIndex:indexPath.row];
     NSString* str = [row objectAtIndex:0];
     NSString* status = [row objectAtIndex:1];
     cell.textLabel.text = str;

     if(cell.detailTextLabel == nil) 
        NSLog(@"detailTextLabel nil");//didn't expect this would execute

     cell.detailTextLabel.text = status;
     cell.detailTextLabel.backgroundColor = [UIColor greenColor];
     NSLog(@"status %@ ",status);
     return cell;
}

Solution

  • If a cell is defined as prototype cell for the table view in a storyboard file, dequeueReusableCellWithIdentifier always instantiates cell as necessary from the storyboard file. In particular, dequeueReusableCellWithIdentifier never return nil.

    You should therefore check the properties of the prototype cell and you can remove the if (cell == nil) part from your code.