重用{J 44}时,UITableViewCell为空白

时间:2015-07-06 08:39:52

标签: ios uitableview

我正在使用UITableView与故事板中定义的各种单元格。风格都是“字幕”。对于一种单元格类型,标识符是“标签”,它在我的表中用于两个位置,因此被取出:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"label" forIndexPath:indexPath];

填充文本标签的代码是:

// cell {0,0} {section,row}
cell.textLabel.text = @"";
cell.detailTextLabel.text = @"Subtitle 1";
NSLog(@"%@",cell); // <UITableViewCell: 0x13ee183c0; frame = (0 26; 320 52); text = ''; ...

// cell {6,0}
cell.textLabel.text = @"Version";
cell.detailTextLabel.text = @""; // using @" " instead cures the problem
NSLog(@"%@",cell); // <UITableViewCell: 0x13ee183c0; frame = (0 1022; 320 52); text = 'Version'; ...

启动时,单元格{0,0}会显示正确的文字。当我向下滚动时,单元格{6,0}显示正确的文本。 但是,当我滚动回到顶部时,单元格{0,0}为空白(重用的cell.text = 'Version')。如果我将单元格{0,0}滚出视图,然后滚动回到顶部,单元格{0,0}就可以了(重用cell.text = '')。

在单元格{6,0}中,如果我对@" "使用@""而非detailTextLabel,则问题就会消失,但单元格中的间距不正确(“版本” “不再是垂直居中的。”

很遗憾cell.text(已弃用的属性)在第二种情况下不为空,但明确将cell.text设置为nil或{{}}中的@""没有任何区别。< / p>

为什么在第6节重用时第一个单元格是空白的?

已编辑添加:

这是我的代码的简化版本,仍然可以证明这个问题。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 10;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *resueIdentifier;
    if ( indexPath.section == 0  &&  indexPath.row == 0 ) {
        resueIdentifier = @"label"; // in the nib, this is a cell with no other controls
    } else if ( indexPath.section == 6  &&  indexPath.row == 0 ) {
        resueIdentifier = @"label";
    } else { // all other rows for demo contain a UISwitch
        resueIdentifier = @"switch"; // in the nib, this is a cell with a UISwitch (e.g., Settings)
    }
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:resueIdentifier forIndexPath:indexPath];
    // never nil -- that always returns a cell from the storyboard/nib

    if ( indexPath.section == 0  &&  indexPath.row == 0 ) {
        cell.textLabel.text = @""; // no title
        cell.detailTextLabel.text = @"This is detail text";
    } else if ( indexPath.section == 6  &&  indexPath.row == 0 ) {
        cell.textLabel.text = @"Version X";
        cell.detailTextLabel.text = @""; // no detail text (this causes the problem)
    } else {
        cell.textLabel.text = @"X";
        cell.detailTextLabel.text = @"Y";
    }
    return cell;
}

要查看此问题,请向下滚动到第6部分(请参阅单元格中的“版本X”),然后滚动回到顶部。顶行将为空(应显示“This is detail text”)。接下来向下滚动,使第0行不在视图中,然后滚动回到顶部(第0行将显示详细文本)。

以下是我的故事板示例(选中“标签”行): Storyboard

2 个答案:

答案 0 :(得分:0)

请使用此:

 static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        NSLog(@"new one");
    }

希望这可以帮助/

答案 1 :(得分:0)

解决方案:在[cell layoutIfNeeded]末尾添加tableView:cellForRowAtIndexPath: (当使用带有各种类型原型单元的故事板时,这可能是一个Apple bug吗?)

建议https://stackoverflow.com/a/27699358/236415