【发布时间】:2017-08-13 03:52:19
【问题描述】:
我有一个自定义的UITableViewCell 子类,我想显示高亮状态但没有选择状态。
如何在触摸时突出显示 UITableViewCell 背景(在发布时取消突出显示),但不显示选择状态单元格背景(我有自己的自定义选择状态)?
【问题讨论】:
标签: ios objective-c swift uitableview uikit
我有一个自定义的UITableViewCell 子类,我想显示高亮状态但没有选择状态。
如何在触摸时突出显示 UITableViewCell 背景(在发布时取消突出显示),但不显示选择状态单元格背景(我有自己的自定义选择状态)?
【问题讨论】:
标签: ios objective-c swift uitableview uikit
@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
【讨论】:
你需要实现UITableView delegate方法
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}
【讨论】:
首先,您需要通过将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
【讨论】: