UITableViewCell textColor will not change with userInteractionEnabled = NO

北城余情 提交于 2019-12-18 08:54:09

问题


I am trying to simply change the color of my UILabel using the textColor property and it will not work when userInteraction is disabled, this does not make any sense, there is nothing in the documentation that mentions that at all. But that is what is happening if I remove that line the color does change, but I do not want user interaction.

UITableViewCellStyleValue1

A style for a cell with a label on the left side of the cell with left-aligned and black text; on the right side is a label that has smaller blue text and is right-aligned. The Settings application uses cells in this style. Available in iOS 3.0 and later.

Both labels are in gray color, and they will not change color. Here is my code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
    }

    //If I remove this line the color changes
    cell.userInteractionEnabled = NO;

    UIColor *blackColor = [UIColor blackColor];

    UILabel *mainLabel = cell.textLabel;
    UILabel *detailTextLabel = cell.detailTextLabel;
    mainLabel.backgroundColor = blackColor;
    detailTextLabel.backgroundColor = blackColor;

    //Trying to change color here but no effect
    mainLabel.textColor = [UIColor redColor];
    detailTextLabel.textColor = [UIColor whiteColor];

    UIView *backView = [[[UIView alloc] initWithFrame:cell.frame] autorelease];
    backView.backgroundColor = blackColor;
    cell.backgroundView = backView;

    mainLabel.text = @"Some text";

    return cell;
}

EDIT: I just realized, it is related to the userInteractionEnabled = NO, when removed the text Color changes, but I do not want user interaction, how can i turn that off but also change the color?, without having to create my own UILabels and adding them to the contentView.


回答1:


Ok I know how to fix this but it does NOT make any sense at all, the problem is this line:

cell.userInteractionEnabled = NO;

If removed the textColor changes, the odd thing is that if i change the user interaction at the end after changing the textColor the color changes!

Is this a bug?. I hope this answer helps anyone trying to figure this out as it does not make any sense at all, and there is no documentation about this.




回答2:


Please check my answer to a similar question:

https://stackoverflow.com/a/18552074/921573

Setting the enabled property on the textLabels accordingly fixes this:

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


来源:https://stackoverflow.com/questions/7793491/uitableviewcell-textcolor-will-not-change-with-userinteractionenabled-no

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!