简体   繁体   English

突出显示没有选择的UITableView

[英]Highlight UITableView Without Selection

I have a custom UITableViewCell subclass and I'd like to show highlight state but no selection state. 我有一个自定义UITableViewCell子类,我想显示突出显示状态,但不显示选择状态。

How can I highlight a UITableViewCell background on touch (and unhighlight on release), but not show the selection state cell background (I have my own custom selection state)? 如何在触摸时突出显示UITableViewCell背景(并在发布时突出显示),而不显示选择状态单元格背景(我有自己的自定义选择状态)?

You need to implement the UITableView delegate method 您需要实现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

First, you need to make the table view unselectable by setting allowsSelection to false . 首先,您需要通过将allowsSelection设置为false来使表视图不可选择。

Next, create this custom table view cell: 接下来,创建此自定义表格视图单元格:

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

    }
}

Use this custom cell in the table view. 在表格视图中使用此自定义单元格。 You can do something like this in cellForRowAtIndexPath : 您可以在cellForRowAtIndexPath

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM