我想在我的UITableView只在某些單元格中顯示圖像。這裏有一個我configureCell方法:有條件地顯示圖像在UITableViewCell
-(void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
StoryInfo *info = [self.fetchedResultsController objectAtIndexPath:indexPath];
UIImage *ribbon = [UIImage imageNamed:@"ribbon.png"];
UIImageView *ribbonView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 15, 15)];
[ribbonView setImage:ribbon];
[cell addSubview:ribbonView];
if([[NSNumber numberWithBool:NO] isEqualToNumber:info.visited]) {
cell.textLabel.textColor = [UIColor colorWithRed:53/255.0 green:53/255.0 blue:52/255.0 alpha:1];
ribbonView.hidden = NO;
}
else {
cell.textLabel.textColor = [UIColor colorWithRed:128.0/255.0 green:128.0/255.0 blue:128.0/255.0 alpha:1.0];
ribbonView.hidden = YES;
}
}
而這裏的cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// set up the cell...
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
這並不完全工作,因爲首先,所有的細胞吸引他們ribbonView,無論info.visited
值。我已經通過了if/else,並且看到它被隱藏的代碼被擊中,但是。但是,如果我從列表中導航,然後返回,則可以看到正確的色帶狀態。滾動表格視圖再次打破它。
雖然字體的顏色總是正確的。
謝謝!這個解決方案和@ pedros的解決方案都可以工作 – hodgesmr 2013-03-16 17:28:59
我錯過了'if(!ribbonView)',這個難題的關鍵部分,我現在也在工作,所以謝謝你的未來! :) – Luke 2013-04-12 11:12:36