开发者

tableView cells wont change font color

I have done many approach on changing font colors from a tableView cell. apparently both method failed. Unless I am doing it wrongly, then I need some help to correct it. Here is the code I found online to change the font color.

[[cell textLabel] setTextColor:[UIColor colorWithRed:255.0/255.0 green:76.0/255.0 blue:76.0/255.0 alpha:1]];

I placed that line in the cellForRowAtIndexPath method and it did not work. Next, I searched this page and found another method to change font colors: tableView:willDisplayCell:forRowAtIndexPath: I copied and paste the setTextColor method into the willDisplayCell method and it did not work either. Below is the source code on which how I attempted and failed:

cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *CellIdentifier = @"ApplicationCell";

    ApplicationCell *cell = (ApplicationCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
    {
        [[NSBundle mainBundle] loadNibNamed:@"PharmacyCell" owner:self options:nil];
        cell = tempCell;
        self.tempCell = nil;
    }

    //cell.useDarkBackground = (indexPath.row % 2 == 0);
    HealthWellness *objHealth = [self.maHealthArray objectAtIndex:indexPath.row];
    cell.strPharmacyName = [objHealth.healthName stringByReplacingOccurrencesOfString:@"|" withString:@""];
    [[cell textLabel] setTextColor:[UIColor colorWithRed:255.0/255.0 green:76.0/255.0 blue:76.0/255.0 alpha:1]];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;
}

tableView:willDisplayCell:forRowAtIndexPath:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    [[cell textLabel] setTextColor:[UIColor colorWithRed:255.0/255.0 green:76.0/255.0 blue:76.0/255.0 alpha:1]];
}

ApplicationCell.h

@interface ApplicationCell : UITableViewCell{
//variable declaration
BOOL useDarkBackground;
BOOL useMenuBackground;
NSString *strPharmacy开发者_运维百科Name;
UIImage *pharmacyIcon;
NSString *strStoreName;}

//setting property @property BOOL useDarkBackground; @property BOOL useMenuBackground; @property(retain) NSString *strPharmacyName; @property(retain) UIImage *pharmacyIcon; @property(retain) NSString *strStoreName;

ApplicationCell.m

- (void)setUseDarkBackground:(BOOL)flag{
if (flag != useDarkBackground || !self.backgroundView)
{
    useDarkBackground = flag;

    NSString *backgroundImagePath = [[NSBundle mainBundle] pathForResource:useDarkBackground ? @"BGDark" : @"BGLight" ofType:@"png"];
    UIImage *backgroundImage = [[UIImage imageWithContentsOfFile:backgroundImagePath] stretchableImageWithLeftCapWidth:0.0 topCapHeight:1.0];
    self.backgroundView = [[[UIImageView alloc] initWithImage:backgroundImage] autorelease];
    self.backgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    self.backgroundView.frame = self.bounds;
}}



- (void)setUseMenuBackground:(BOOL)flag{
if (flag != useDarkBackground || !self.backgroundView)
{
    useDarkBackground = flag;
    NSString *backgroundImagePath = [[NSBundle mainBundle] pathForResource:useDarkBackground ? @"BGDark" : @"BGLight" ofType:@"png"];
    UIImage *backgroundImage = [[UIImage imageWithContentsOfFile:backgroundImagePath] stretchableImageWithLeftCapWidth:0.0 topCapHeight:1.0];
    self.backgroundView = [[[UIImageView alloc] initWithImage:backgroundImage] autorelease];
    self.backgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    self.backgroundView.frame = self.bounds;

}}


You seem to be using a custom subclass of UITableViewCell and since you mention that there is a lot going on in the ApplicationCell class. I am guessing that your subclass isn't making use of the textLabel field since your changes aren't reflecting on the UI.

Check for the property outlet that is being displayed on screen from your ApplicationCell interface declaration & the NIB file. Once you've the appropriate property outlet, change its property.


That should set the text color, I just tried it and it works. cell should be a UITableView though, does your ApplicationCell inherit from UITableView cell? Also if ApplicationCell is nil, I'm not sure whats going on with tempCell

try

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];

and go from there

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜