【问题标题】:tableView cells wont change font colortableView 单元格不会更改字体颜色
【发布时间】:2011-09-23 05:36:06
【问题描述】:

我已经做了很多方法来改变 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 使用菜单背景; @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;

}}

【问题讨论】:

  • 您在哪里创建 tempCell
  • 可以发一下ApplicationCell的界面吗?
  • 它是一个已创建的自定义单元格。对不起,这个项目是传下来的,所以我不是代码的合法所有者。
  • ApplicationCell 代码太长了,要我回答我自己的问题吗?还是继续编辑问题?
  • 最有趣的改变单元格颜色的方法是使用UILabel

标签: iphone uitableview fonts ios4


【解决方案1】:

您似乎正在使用UITableViewCell 的自定义子类,因为您提到ApplicationCell 类中发生了很多事情。我猜您的子类没有使用 textLabel 字段,因为您的更改没有反映在 UI 上。

ApplicationCell 接口声明和NIB 文件中检查正在屏幕上显示的属性插座。一旦你有了合适的属性插座,就改变它的属性。

【讨论】:

  • 感谢 Deepak 的快速回复。我稍后会试一试。现在我的老板想要我别的东西:(
  • 好的,我已经编辑了这个问题。我插入了 ApplicationCell
  • @Melvin Lai 在您的ApplicationCell 代码中是否提到了UILabeltextlabel
【解决方案2】:

这应该设置文本颜色,我刚刚尝试过,它可以工作。单元格应该是 UITableView,但您的 ApplicationCell 是否继承自 UITableView 单元格? 此外,如果 ApplicationCell 为零,我不确定 tempCell 发生了什么

试试

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

从那里出发

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-24
    • 2011-10-23
    • 2013-03-15
    • 2012-01-04
    • 2014-07-15
    • 1970-01-01
    • 1970-01-01
    • 2014-08-24
    相关资源
    最近更新 更多