Home > DeveloperSection > Forums > How to change background color of UILabel in UITableViewCell
Derek Honeybun
Derek Honeybun

Total Post:17

Points:119
Posted on    December-08-2015 10:48 PM

 iPhone iPhone  Ios  Objective C 
Ratings:


 1 Reply(s)
 268  View(s)
Rate this:

I have created an app on iPhone using UITableViewCell within table view and I put an UILabel into table view cell.
I want to change the background color of UILabel.
this is my code:

UILabel *myLabel = (UILabel*)[cell.contentView.subviews objectAtIndex:0];
myLabel.textColor = [UIColor blueColor];
mylabel.backgroundColor = [UIColor greyColor];
mylabel.opaque = YES;

Now, please anyone tell me the problem in my code.

Thanks in advance..!



Tarun Kumar

Total Post:210

Points:1614
Supported
Posted on    December-09-2015 11:42 PM

Your problem is that you are not adding your own label to the contentView at the time of allocating the cell. Use addSubview: method to allocate it on the cell.
Here we provide your complete code with one additional line of code:

UILabel *myLabel = (UILabel*)[cell.contentView.subviews objectAtIndex:0];
myLabel.textColor = [UIColor blueColor];
mylabel.backgroundColor = [UIColor greyColor];
mylabel.opaque = YES;
[cell.contentView addSubview:myLabel];

Modified On Dec-09-2015 11:42:49 PM

Don't want to miss updates? Please click the below button!

Follow MindStick