UITableViewCell和userInteractionEnabled的奇怪iOS错误

时间:2012-07-25 16:39:20

标签: ios uitableview

我刚注意到iOS上的UITableViewCell类和userInteractionEnabled属性有些奇怪。

如果在将文本分配给单元格标签之前将userInteractionEnabled设置为NO ,则该文本显示为灰色。但是,在设置文本后将userInteractionEnabled设置为NO 会使文本颜色保持黑色(请参阅下面的示例代码片段)。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell)
       cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

    // swap these two lines around, and the text color does not change to grey!
    cell.userInteractionEnabled = (indexPath.row % 2) == 0;
    cell.textLabel.text = @"Hello";

    return cell;
}

这真的很烦人,因为这意味着在重复使用单元格的情况下,我最终会遇到不同的行为。上面的示例演示了这一点 - 表格的第一页显示了带有灰色/黑色文本的备用行。向下滚动以便细胞重复使用,您可以看到出现问题。

我只是想知道我做错了什么,或者这是一个iOS错误?我在iPad 3上看到iOS 5.1下的问题。任何见解都非常感谢!

2 个答案:

答案 0 :(得分:1)

我发现,如果我在cell.textLabel.textColor = [UIColor blackColor];之前放置cell.userInteractionEnabled = NO;,它似乎可以解决问题。这就是它在iOS 6.0.1上的工作方式

cell.textLabel.textColor = [UIColor blackColor];
cell.userInteractionEnabled = NO;

答案 1 :(得分:1)

我认为我找到了一个更方便的解决方法(我认为这是一个错误):

enabled属性设置为textLabeldetailTextLabel,如下所示:

cell.userInteractionEnabled = (indexPath.row % 2) == 0;
cell.textLabel.enabled = cell.isUserInteractionEnabled;
cell.detailTextLabel.enabled = cell.isUserInteractionEnabled;

这让我得到了答案: https://stackoverflow.com/a/13327632/921573