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
.
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.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With