【发布时间】: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