我有這個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;
}
爲什麼使用UILabel的幀矩形爲{0,0,0,0}? –
你可以在你的問題中顯示'cellForRowAtIndexPath:'方法的代碼嗎? –
@邁克爾感謝您的幫助。我編輯了這個問題向你展示了cellForRowAtIndexPath方法。但我不認爲這會有所幫助,因爲我有一個TableViewCell.m,我試圖配置這個... – Farini