【问题标题】:Highlight UITableView Without Selection突出显示没有选择的 UITableView
【发布时间】:2017-08-13 03:52:19
【问题描述】:

我有一个自定义的UITableViewCell 子类,我想显示高亮状态但没有选择状态。

如何在触摸时突出显示 UITableViewCell 背景(在发布时取消突出显示),但不显示选择状态单元格背景(我有自己的自定义选择状态)?

【问题讨论】:

    标签: ios objective-c swift uitableview uikit


    【解决方案1】:
    @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
    

    【讨论】:

      【解决方案2】:

      你需要实现UITableView delegate方法

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

      【讨论】:

      • 这实际上会取消选择单元格——我只想要一个自定义状态。
      【解决方案3】:

      首先,您需要通过将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
      

      【讨论】:

        猜你喜欢
        • 2012-02-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-23
        • 1970-01-01
        相关资源
        最近更新 更多