【问题标题】:Can the imageView of UITableViewCell be set separately for selected and highlighted states?UITableViewCell 的 imageView 可以单独设置选中状态和高亮状态吗?
【发布时间】:2012-02-02 22:30:43
【问题描述】:

在 UIButton 中,以下方法明确允许为不同的状态设置不同的图像:

// default is nil. should be same size if different for different states
- (void)setImage:(UIImage *)image forState:(UIControlState)state;

(1) UITableViewCell 也可以做类似的事情吗?例如,将某些状态切换为 true,然后设置 imageView?

// set selected state (title, image, background). default is NO. animated is NO
@property(nonatomic,getter=isSelected) BOOL         selected;
// set highlighted state (title, image, background). default is NO. animated is NO
@property(nonatomic,getter=isHighlighted) BOOL      highlighted;

(2) 更改会持续存在还是会简单地覆盖单一的 imageView?

(3) 在表格单元格中模拟每个状态的图像行为的最佳方法是什么?

【问题讨论】:

    标签: iphone ios4 uitableview uibutton


    【解决方案1】:
    1. 无需实现这些布尔值。 UITableViewCell 有一个 'selected' 属性,当用户选择一个单元格时,您可以将其设置为 YES。

    2. 您的UITableViewDelegate 回调 - tableView didSelectRowAtIndexPath 应该用于设置单元格的图像(我通过设置作为子视图添加到单元格的 contentView 的 UIImageView 的图像来实现),如图所示:

      [cell.contentView addSubview:yourImageView];

    您可能希望跟踪设置为选定状态的最后一个单元格。这可以通过存储先前选择的单元格的 NSIndexPath 来完成。在 didSelectRowForIndexPath 中,先读取这个 indexpath,抓取对应的单元格并将其图像设置为未选中的那个。

    我建议为此子类化 UITableViewCell,以便您可以实现一个方法,该方法将 UIImageView 作为参数并将其设置为 UIImageView 设置为单元格的背景图像。

    代码应该是这样的:

    你的子类 UITableViewCell 中的图片设置方法:

    - (void)setCellBackgroundImage:(UIImage*)inImage
    {
        backgroundImageView.image = inImage;
    }
    

    请注意,'backgroundImageView 是您将在子类 UITableViewCell 中使用的 UIImageView ivar。设置单元格时,将此图像视图添加到单元格的 contentView 中,如前所示。

    使用该方法设置单元格后,您的 didSelectRowAtIndexPath 方法应如下所示(请注意,oldSelectedCell 的类型为 NSIndexPath ,其中包含旧选定单元格的索引路径):

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UIImage *selectedImage = [UIImage imageNamed:@"path/to/your/selected/image.png"];
        UIImage *deSelectedImage = [UIImage imageNamed:@"path/to/your/deselected/image.png"];
    
        if(oldSelectedCell != nil)
        {
            //Assuming SubclassedCell is the name of your subclass of UITableViewCell
            SubclassedCell *cell = (SubclassedCell*)[tableView cellForRowAtIndexPath:oldSelectedCell];
    
            //Set the previously selected cell's image to be the deselected image
            [cell setCellBackgroundImage:deSelectedImage];
    
            //Set the cell to be deselected
            cell.selected = NO;
        }
    
        //Set the current cell's image
        SubclassedCell *cell = (SubclassedCell*)[tableView cellForRowAtIndexPath:indexPath];
    
        [cell setBackgroundImage:selectedImage];
        cell.selected = YES;
    
        //Save this cell's indexpath to be the previously selected cell for the next time this method is invoked
        oldSelectedCell = indexPath;
    
        //And you're done!
    }
    

    单元格的 'selected' 属性可用于在子类单元格的 layoutSubviews 实现中随意设置其视图。检查该属性的值并根据需要设置单元格。

    【讨论】:

    • 哇,感谢您的详尽解释!我已经阅读并理解它,所以我会在它生效并测试它是否有效后投票:)
    猜你喜欢
    • 2012-08-17
    • 1970-01-01
    • 2011-11-05
    • 2020-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-25
    • 1970-01-01
    相关资源
    最近更新 更多