0

我有这个 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;
}
4

1 回答 1

0

是的!我知道我错过了一些简单的东西。我所要做的就是将 If... 语句“移动”到单元格的实现设置文本的位置。只有在那里它才会更新颜色(或其他任何东西)所以这是我现在完美运行的最终代码......

- (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;
if ([account.income isEqualToString:@"income"]){
    [priceLabel setTextColor:[UIColor greenColor]];
} else if ([account.income isEqualToString:@"expense"]) {
    [priceLabel setTextColor:[UIColor redColor]];
} //this will give me green text color for income and red text color for expense
}

特别感谢 Michael Dauterman,他激励我彻夜不眠,直到我的功能真正发挥作用。

于 2011-11-26T20:49:56.793 回答