【问题标题】:Make UITableViewCell flash briefly让 UITableViewCell 短暂闪烁
【发布时间】:2019-04-02 22:18:25
【问题描述】:

我有一个UITableView 设置,我正在探索突出显示UITableViewCell 的所有不同选项,我想知道以下几点:

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        .
        .
    cell.selectionStyle = .gray
    return cell
}

What happens now is that when a cell is selected it is highlighted with a grey color, I was wondering, is it possible to deselect the cell after about 0.5 sec?

基本上是否可以使单元格仅短暂闪烁,以便用户知道他与单元格进行了交互,但它不会保持选中状态?我将此 tableView 用于侧边菜单,并且保持突出显示不是我想要的行为。

谢谢!

【问题讨论】:

    标签: ios swift uitableview highlight visual-effects


    【解决方案1】:

    您可以在didSelectRow 方法上取消选择该行,单元格将突出显示,然后选择将淡出,让用户知道他们与该单元格进行了交互。

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
      tableView.deselectRow(at: indexPath, animated: true)
      // Do whatever else you need
    }
    

    【讨论】:

      【解决方案2】:

      终于让它工作了,我所做的是:

      func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
       tableView.cellForRow(at: indexPath)?.backgroundColor = .gray
          DispatchQueue.main.asyncAfter(deadline: .now()) {
              UIView.animate(withDuration: 0.5, animations: {
                  tableView.cellForRow(at: indexPath)?.backgroundColor = .none
              })
          }
      

      也许这会对其他人有所帮助。唯一的问题是,点击的单元格在按下时不会突出显示,只有在手势结束后(即仅点击)。如果有人对此有解决方案,我将不胜感激,但现在就可以了。

      【讨论】:

      • 首先,我猜DispatchQueue.main.asyncAfter(deadline: .now())是没有必要的。 UIView.animate 应该在主线程上运行。无论如何,Emilios 的答案可能更正确,因为它取消选择单元格,你的只会改变背景。
      • 这种方式对我有用,因为我只将 tableView 用于侧边菜单,但我理解从干净的代码角度来看,如果我不这样做,将来最好取消选择该行t 需要选择单元格。谢谢!
      猜你喜欢
      • 1970-01-01
      • 2015-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多