我已经做了很多方法来改变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
方法中,但它也不起作用。以下是我尝试和失败的源代码:
- (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;
}
- (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]];
}
@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;
- (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;
}}
答案 0 :(得分:2)
您似乎正在使用UITableViewCell
的自定义子类,因为您提到ApplicationCell
类中有很多内容。我猜你的子类没有使用textLabel
字段,因为你的更改没有在UI上反映出来。
从ApplicationCell
界面声明&中检查屏幕上显示的媒体插座。 NIB
文件。一旦你有了合适的房产出口,就改变它的财产。
答案 1 :(得分:0)
那应该设置文本颜色,我只是尝试了它,它的工作原理。 cell应该是UITableView,你的ApplicationCell是否继承自UITableView单元? 此外,如果ApplicationCell为零,我不确定是否会使用tempCell
试
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
从那里开始