[英]Highlight UITableView Without Selection
我有一个自定义UITableViewCell
子类,我想显示突出显示状态,但不显示选择状态。
如何在触摸时突出显示UITableViewCell
背景(并在发布时突出显示),而不显示选择状态单元格背景(我有自己的自定义选择状态)?
您需要实现UITableView delegate
方法
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}
@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
首先,您需要通过将allowsSelection
设置为false
来使表视图不可选择。
接下来,创建此自定义表格视图单元格:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.