2017-08-13 100 views
0

我有一个自定义UITableViewCell子类,我想显示突出显示状态,但没有选择状态。突出显示UITableView没有选择

如何突出显示UITableViewCell触摸背景(并且释放时不高亮),但不显示选择状态单元背景(我有自己的自定义选择状态)?

回答

0
@implementation CustomTableViewCell 

- (void)awakeFromNib { 
    [super awakeFromNib]; 
    // Initialization code 
} 

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

    // You have your code here as you said 
} 

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated 
{ 
    //Implement custom hilighting code 
} 

@end 
0

您需要实现UITableView delegate方法

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    tableView.deselectRow(at: indexPath, animated: true) 
} 
+0

这实际上取消though-我只是想自定义状态的细胞。 – arooo

0

首先,你需要做的表视图不可选择通过设置allowsSelectionfalse

接下来,创建这个定制表格视图单元格:

class MyCell: UITableViewCell { 
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
     backgroundColor = .red // or some other color 
    } 

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 
     backgroundColor = .white 

    } 
} 

使用表格视图此自定义单元格。你可以做这样的事情在cellForRowAtIndexPath

let cell = MyCell() 
cell.isUserInteractionEnabled = true 
// configure the cell's other stuff 
return cell