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 に答える