我有带有style = UITableViewCellStyleValue1的UITableViewCell单元格(在我的项目中使用的所有其他view controllers中都使用这种样式)。根据其他SO帖子的建议,我已经仔细检查了我的故事板中的cell identifiers。尽管如此,我仍然将detailTextLabel设置为零。我还有什么地方做错了吗?我观察到单元格总是被重用,也就是分配的if语句从来没有执行过。那么,如何获得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;
}发布于 2013-06-14 08:56:45
如果将单元格定义为情节提要文件中表格视图的原型单元格,则dequeueReusableCellWithIdentifier始终会根据需要实例化情节提要文件中的单元格。特别是,dequeueReusableCellWithIdentifier从不返回nil。
因此,您应该检查原型单元格的属性,并且可以从代码中删除if (cell == nil)部件。
https://stackoverflow.com/questions/17099412
复制相似问题