【问题标题】:Changing alpha of UITableViewCell when selected does nothing选择时更改 UITableViewCell 的 alpha 什么都不做
【发布时间】:2015-10-01 15:28:37
【问题描述】:

按照这个答案Custom UITableViewCell selection style? 中关于如何为UITableViewCell 制作自定义选择样式的建议,我执行了以下操作。

首先,在我的自定义视图控制器中,它有一个 UITableView 作为属性并用作它的数据源和委托,我有以下内容:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    //UITableViewCell *cell;
    NavTableViewCell *cell;
    cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

那么这就是我的整个自定义 UITableViewCell:

#import "NavTableViewCell.h"

@implementation NavTableViewCell

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
    [super setHighlighted:highlighted animated:animated];
    if(highlighted){
        self.contentView.alpha = .5;
    }else{
        self.contentView.alpha = 1;

    }
}

@end

我也尝试将代码放入setSelected。在这两种情况下,我都没有看到我选择/突出显示的表格视图单元格有任何变化。我试过改变它的颜色和改变它里面标签的颜色,但根本没有改变。我还尝试过查看单元格的各种选择样式,但就看到我的自定义而言,一切都无济于事。

我做错了什么?

【问题讨论】:

    标签: ios objective-c uitableview


    【解决方案1】:

    来自文档:

    您可以使用 selectedBackgroundView 为单元格提供自定义外观 选择。当单元格被选中时,这个视图在 backgroundView 和 contentView 后面。

    因此,无论您在选择单元格时要对单元格做什么,都必须为selectedBackgroundView 执行此操作。

    【讨论】:

      【解决方案2】:

      试试这个:

      UIView *highlightView = [[UIView alloc] init];
      highlightView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:1.0 alpha:.5];
      cell.selectedBackgroundView = highlightView;
      

      【讨论】:

      • 感谢您的建议。这做了一些事情,但没有达到我想要的效果,即整个 contentView 和封闭的子视图改变它们的 alpha 值。
      猜你喜欢
      • 2013-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-26
      • 2010-12-23
      • 1970-01-01
      相关资源
      最近更新 更多