0

我有UITableViewCell单元格style = UITableViewCellStyleValue1(这种样式用于view controllers我在项目中使用的所有其他样式)。cell identifiers根据其他 SO 帖子的建议,我已经仔细检查了我的故事板中的内容。我仍然得到detailTextLabel零。还有什么我做错了吗?我观察到单元总是被重用,即永远不会执行分配语句。那么,如何让我UITableViewCell的对象的样式来验证重复使用的单元格是否遵循相同的样式?

- (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;
}
4

1 回答 1

0

如果一个单元格被定义为故事板文件中表格视图的原型单元格,则 dequeueReusableCellWithIdentifier始终根据需要从故事板文件中实例化单元格。特别是,dequeueReusableCellWithIdentifier永远不要返回nil

因此,您应该检查原型单元的属性,并且可以 if (cell == nil)从代码中删除该部分。

于 2013-06-14T00:56:44.827 回答