2011-11-26 69 views
0

我有這個tableviewcell.m來配置我的單元格。 它正確地配置文本和信息,但我不能讓它根據其他信息更新顏色。 下面的代碼:TableViewCell不能正確更新

- (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; 
} 
+1

爲什麼使用UILabel的幀矩形爲{0,0,0,0}? –

+0

你可以在你的問題中顯示'cellForRowAtIndexPath:'方法的代碼嗎? –

+0

@邁克爾感謝您的幫助。我編輯了這個問題向你展示了cellForRowAtIndexPath方法。但我不認爲這會有所幫助,因爲我有一個TableViewCell.m,我試圖配置這個... – Farini

回答

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 
} 

特別感謝邁克爾Dauterman誰啓發了我保持清醒走夜路小時直到我的功能實際上工作。

+0

我無法接受或投票我的答案,但它的工作... – Farini