有条件地在UITableViewCell中显示图像

时间:2013-03-16 16:08:54

标签: ios objective-c uitableview

我正在尝试在UITableView中的某些单元格中显示图像。这是我的configureCell方法:

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

这不太有效,因为首先,无论info.visited的值如何,所有单元格都会将它们绘制为ribbonView。我已经逐步完成了if / else,但我看到了它被隐藏的代码。但是,如果我离开列表然后返回,则可以看到正确的功能区状态。滚动表格视图会再次打破它。

但字体颜色总是正确的。

3 个答案:

答案 0 :(得分:5)

如果您正在重复使用单元格,那么可能是您要向单元格添加多个ribbonView子视图,因此即使当前info.visited的{​​{1}}为{{1} ,你仍然可以看到另一个单元格上的ribbonView剩余物。

要做的是确保您只有一个indexPath子视图,可以通过在配置方法中删除旧的NO来完成,或者通过子类化ribbonView更好地完成向单元格添加ribbonViews属性,该属性设置一次并添加到单元格的视图层次结构中,然后您可以访问该属性并将UITableViewCell设置为ribbonViewhidden在配置方法中。

编辑:单元格文本颜色将始终正确,因为您正在更改单元格视图层次结构中NO的一个实例上的颜色。如果您的配置方法在每次配置时向单元格添加了新的YES子视图,我希望您会看到相同的错误行为。

编辑:代码尝试

UILabel

答案 1 :(得分:1)

如果您在dequeueReusableCellWithIdentifier:forIndexPath:方法中使用tableView:cellForRowAtIndexPath:。每次重新使用单元格时,都会创建一个新的UIImageView并将其放在最后一个上。

但要解决这个问题,你不需要继承。还没有,因为你的细胞还很简单。如果要添加更多子视图,则子类化是唯一的选择。

我能想到的一个解决方案是:

-(void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
    StoryInfo *info = [self.fetchedResultsController objectAtIndexPath:indexPath];

    UIImageView *ribbonView = nil;

    //My code:
    for ( UIView *childView in cell.subviews ) {
        if([childView isKindOfClass:[UIImageView class]] {
            ribbonView = childView;
            break;
        }
    }
    //Note: this doesnt work if you have more than one UIImageView in your cell. 
    if(ribbonView == nil) {
        UIImage *ribbon = [UIImage imageNamed:@"ribbon.png"];
        ribbonView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 15, 15)];
        [ribbonView setImage:ribbon];
        [cell addSubview:ribbonView];
    }
    //Ends here.

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

尝试并告诉我它是否有效。

祝你好运。

答案 2 :(得分:0)

这绝对是

实施中的一个问题
tableView:cellForRowAtIndexPath:

您需要始终致电

dequeueReusableCellWithIdentifier:forIndexPath:

然后调用您的configure方法

configureCell:atIndexPath:(NSIndexPath *)indexPath

修改

另外,我认为你还需要做[cell.contentView addSubView:...]而不是[cell addSubView:...]