セルを構成するために、この 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;
}