UITableViewCell和userInteractionEnabled的奇怪的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; } 

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

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

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

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

我想我找到了一个更方便的解决方法来解决这个问题(我认为这是一个bug):

手动设置textLabeldetailTextLabelenabled属性,如下所示:

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

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