选择时更改UITableViewCell的alpha不会

时间:2015-10-01 15:28:38

标签: ios objective-c uitableview

根据此回答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中。在这两种情况下,我都没有看到我选中/突出显示的表格视图单元格中的任何更改。我尝试过更改颜色并更改其中标签的颜色,但根本没有变化。我也尝试过为单元格选择所有类型的选择样式,只要看到我的自定义就无济于事。

我做错了什么?

2 个答案:

答案 0 :(得分:7)

来自文档:

  

您可以使用 selectedBackgroundView 为单元格提供自定义外观   选择。选择单元格后,此视图位于上方   backgroundView和在contentView后面。

因此,无论您在选择单元格时想要对单元格执行什么操作,都必须为<video autoplay poster="" id="bgvid" loop> <source src="?" type="video/webm"> <source src="?" type="video/mp4"> </video> 执行此操作。

答案 1 :(得分:0)

试试这个:

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