如何取消tableView点击cell的选中效果

tableView我们经常要用到,然后很多地方我们并不需要点击cell的时候有带灰的选中效果,那么这里我介绍一下3种方法  如何取消cell的选中效果

1,通过更改tableViewCell的selectionStyle属性进行设置(自己亲测了一下,只有none和default两种style可用):

自定义cell里面:

- (void)awakeFromNib {
    [super awakeFromNib];

    self.selectionStyle = UITableViewCellSelectionStyleNone;
}

2,设置tableView的allowsSelection属性   注意:属性为NO的时候   tableView的代理方法: 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 就不会执行 ,所以如果需要有cell点击事件的  不要用此方法

tableView.allowsSelection = NO; 

3,更改cell属性selectedBackgroundView 的背景色

我们选中cell时有灰色效果就是因为selectedBackgroundView这个家伙,所以,我们可以通过设置改View的背景色来改变选中颜色

self.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];  
self.selectedBackgroundView.backgroundColor = [UIColor redColor]; 

 注意点:

1, 在storyboard设置tableView属性的时候   如果这样设置   selection: NoSelection   会导致点击cell的时候没反应  不会调用 didSelectRowAtIndexPath 方法   要想有点击方法应设置成 selection: SingleSelection

原文地址:https://www.cnblogs.com/yulongjiayuan/p/6839794.html