Question

I have this tableviewcell.m to configure my cells. It is configuring the text and information correctly but I just can't get it to update the color according to some other information. Here's the code:

- (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
}

The color though, not working at all. It seems to me that the if statement is being completely ignored. Could anyone help me to understand why or suggest a better programming approach ?

Here's the cell for row code. I don't know if it will help because they aren't in the same file. Here I'm only referencing my tableviewcell file:

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

Many thanks !

Here's the code where i get the texts:

- (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;
}
Was it helpful?

Solution

Yes! I knew I was missing something simple. All I had to do is "move" that If... statement to where the implementation of the cell is setting the text. There and only there it will update the color (or anything else) so here's my final code that's working perfectly now...

- (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
}

Special thanks for Michael Dauterman who inspired me to stay awake for hours through the night 'till my function is actually working.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top