【发布时间】:2011-11-26 11:03:49
【问题描述】:
我有这个 tableviewcell.m 来配置我的单元格。 它正在正确配置文本和信息,但我无法让它根据其他一些信息更新颜色。 代码如下:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
priceLabel = [[UILabel alloc] initWithFrame:CGRectZero];
priceLabel.textAlignment = UITextAlignmentRight;
[priceLabel setFont:[UIFont systemFontOfSize:12.0]];
if ([account.income isEqualToString:@"income"]){
[priceLabel setTextColor:[UIColor greenColor]];
} else if ([account.income isEqualToString:@"expense"]) {
[priceLabel setTextColor:[UIColor redColor]];
} //label, alignment, font are working correctly
//but the if statement doesn't get there
}
但是颜色,根本不起作用。 在我看来,if 语句被完全忽略了。 谁能帮我理解原因或提出更好的编程方法?
这是行代码的单元格。我不知道这是否会有所帮助,因为它们不在同一个文件中。这里我只引用了我的 tableviewcell 文件:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// create a TableViewCell, then set its account to the account for the current row.
static NSString *AccountCellIdentifier = @"AccountCellIdentifier";
TableViewCell *accountCell = (TableViewCell *)[tableView dequeueReusableCellWithIdentifier:AccountCellIdentifier];
if (accountCell == nil) {
accountCell = [[[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AccountCellIdentifier] autorelease];
accountCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
[self configureCell:accountCell atIndexPath:indexPath];
return accountCell;
}
//and here the cell config
- (void)configureCell:(TableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
// Configure the cell
Account *account = (Account *)[fetchedResultsController objectAtIndexPath:indexPath];
cell.account = account;
}
非常感谢!
这是我获取文本的代码:
- (void)setAccount:(Account *)newAccount {
if (newAccount != account) {
[account release];
account = [newAccount retain];
}
nameLabel.text = account.name;
accountLabel.text = Account.accounttype;
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
NSString *stringcost = [numberFormatter stringFromNumber:account.cost];
priceLabel.text = stringcost;
}
【问题讨论】:
-
你为什么使用框架矩形为 { 0, 0, 0, 0 } 的 UILabel ???
-
你能在上面的问题中显示
cellForRowAtIndexPath:方法的代码吗? -
@Michael 感谢您迄今为止的帮助。我编辑了问题以向您展示 cellForRowAtIndexPath 方法。但我认为这不会有帮助,因为我有一个 TableViewCell.m 我正在尝试配置它......
-
你是对的......它没有帮助。我真正想知道的是,您在代码中的哪个位置将文本值分配给 TableViewCell 中的
priceLabelUILabel 以及 该代码 是什么样的。还有一件事。请确保您粘贴到问题中的代码格式正确(检查文本输入窗口下方的预览)。我现在不得不清理你粘贴两次的代码。w -
刚刚更新再次显示我设置文本的位置。感谢您的帮助!
标签: xcode4 uitableview ios5 nscell