tableView单元格不会更改字体颜色

时间:2011-06-27 04:27:33

标签: iphone uitableview fonts ios4

我已经做了很多方法来改变tableView单元格中的字体颜色。显然两种方法都失败了。除非我做错了,否则我需要一些帮助来纠正它。这是我在网上找到的用于更改字体颜色的代码。

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

我将该行放在cellForRowAtIndexPath方法中,但它不起作用。接下来,我搜索了这个页面并找到了另一种更改字体颜色的方法:tableView:willDisplayCell:forRowAtIndexPath:我将setTextColor方法复制并粘贴到willDisplayCell方法中,但它也不起作用。以下是我尝试和失败的源代码:

的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 *strPharmacyName;
UIImage *pharmacyIcon;
NSString *strStoreName;}

//设置属性 @property BOOL useDarkBackground; @property BOOL useMenuBackground; @property(retain)NSString * strPharmacyName; @property(保留)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;

}}

2 个答案:

答案 0 :(得分:2)

您似乎正在使用UITableViewCell的自定义子类,因为您提到ApplicationCell类中有很多内容。我猜你的子类没有使用textLabel字段,因为你的更改没有在UI上反映出来。

ApplicationCell界面声明&中检查屏幕上显示的媒体插座。 NIB文件。一旦你有了合适的房产出口,就改变它的财产。

答案 1 :(得分:0)

那应该设置文本颜色,我只是尝试了它,它的工作原理。 cell应该是UITableView,你的ApplicationCell是否继承自UITableView单元? 此外,如果ApplicationCell为零,我不确定是否会使用tempCell

  

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

从那里开始