UITableViewCell和userInteractionEnabled的奇怪的iOS错误 [英] Weird iOS bug with UITableViewCell and userInteractionEnabled

查看:307
本文介绍了UITableViewCell和userInteractionEnabled的奇怪的iOS错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I just noticed something very strange with the UITableViewCell class on iOS and the userInteractionEnabled property.

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

It appears that if userInteractionEnabled is set to NO before assigning text to the cell label, then the text is coloured grey. However, setting userInteractionEnabled to NO after the text has been set leaves the text colour as black (see the example code fragment below).

- (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;
}

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

This is really annoying, because it means that I end up with a different behaviour in the case that a cell is reused. The above example demonstrates this - the first page of the table shows alternate rows with grey/black text. Scroll further down so that cells get reused, and you can see that things go wrong.

我只是想知道我做错了什么,或者这是不是一个iOS bug ?我在iPad 3上看到了iOS 5.1下的问题。任何见解真的很感激!

I just wondered if I am doing something wrong, or if this is an iOS bug? I see the problem under iOS 5.1 on the iPad 3. Any insight really appreciated!

推荐答案

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

I think I found a more convenient workaround for this problem (which I consider to be a bug):

在<$ c $上设置启用属性c> textLabel 和 detailTextLabel 手动如下:

Set the enabled property on textLabel and detailTextLabel manually like this:

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

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

这篇关于UITableViewCell和userInteractionEnabled的奇怪的iOS错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆