HOW TO CHANGE BACKGROUND COLOR OF UILABEL IN UITABLEVIEWCELL

Derek Honeybun

Total Post:17

Points:119
Posted by  Derek Honeybun
 847  View(s)
Ratings:
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..!

  1. Tarun Kumar

    Post:214

    Points:1642
    Re: How to change background color of UILabel in UITableViewCell

    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

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!