UITableViewCell和userInteractionEnabled的奇怪iOS错误

Mat*_*ate 9 uitableview ios

我刚刚注意到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;
}
Run Code Online (Sandbox Code Playgroud)

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

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

Chr*_*ris 1

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

cell.textLabel.textColor = [UIColor blackColor];
cell.userInteractionEnabled = NO;
Run Code Online (Sandbox Code Playgroud)