首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TableViewCell未正确更新

TableViewCell未正确更新
EN

Stack Overflow用户
提问于 2011-11-26 19:03:50
回答 1查看 165关注 0票数 0

我有这个tableviewcell.m来配置我的单元。它正确地配置了文本和信息,但我就是不能让它根据其他信息更新颜色。代码如下:

代码语言:javascript
复制
- (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文件:

代码语言:javascript
复制
- (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; 
}

非常感谢!

下面是我获取文本的代码:

代码语言:javascript
复制
- (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;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-11-27 04:49:57

是!我知道我错过了一些简单的东西。我要做的就是“移动”如果...语句设置为单元格的实现设置文本的位置。在那里,也只有在那里,它会更新颜色(或其他任何东西),所以这是我的最终代码,它现在工作得很好…

代码语言:javascript
复制
- (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,他激励我彻夜不眠,直到我的功能真正发挥作用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8277982

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档