系統自定義的點擊背景有時間覺得效果不好想換個
- (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; if (selected) { self.backgroundColor = [UIColor redColor]; }else { self.backgroundColor = [UIColor whiteColor]; } }
如果你發現時上面的,0-0那肯定沒成功啊,為什麼呢?
打開層級,可以看到,原來在下面,這樣就簡單了,只需要在初始化cell後加上這麼一段代碼
cell.selectionStyle = UITableViewCellSelectionStyleNone;
效果圖:
-0-成功了~~~~~~~~~~~~~~~~~~~
add...
如果想實現閃一下而不是一直選擇的話,可以用tablview的代理實現
首先:
- (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; if (selected) { self.backgroundColor = [UIColor whiteColor]; }else { self.backgroundColor = [UIColor whiteColor]; } }
在tablview的代理中:
- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; cell.backgroundColor = [UIColor greenColor]; }
效果圖:
so-簡單方法,自定義cell裡面,可以實現一樣的效果,也不用在外面寫了
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated { [super setHighlighted:highlighted animated:animated]; if (highlighted) { self.backgroundColor = [UIColor redColor]; }else { self.backgroundColor = [UIColor whiteColor]; } }