【问题标题】:TableViewCell doesn't update correctlyTableViewCell 没有正确更新
【发布时间】: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 中的 priceLabel UILabel 以及 该代码 是什么样的。还有一件事。请确保您粘贴到问题中的代码格式正确(检查文本输入窗口下方的预览)。我现在不得不清理你粘贴两次的代码。w
  • 刚刚更新再次显示我设置文本的位置。感谢您的帮助!

标签: xcode4 uitableview ios5 nscell


【解决方案1】:

是的!我知道我错过了一些简单的东西。 我所要做的就是将 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,他激励我彻夜不眠,直到我的功能真正发挥作用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多