2012-07-25 93 views
9

我只注意到iOS上的UITableViewCell类和userInteractionEnabled属性非常奇怪的东西。奇怪的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下的问题。任何有识之士都非常感谢!

+1

如果哟你有一个可重现的错误(特别是对于一些示例代码),那么请将其报告给苹果:https://developer.apple.com/bugreporter/ – 2012-07-25 17:05:18

+0

我向苹果公司报告过(相当长一段时间),顺便说一句。还没有听到任何... – 2012-10-03 13:12:32

回答

1

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

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

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

设置上textLabelenabled财产detailTextLabel手动像这样:

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

这使我的答案: https://stackoverflow.com/a/13327632/921573