【问题标题】:Why do my UITableViewCells turn grey when I tap on them?为什么我的 UITableViewCells 在我点击它们时会变成灰色?
【发布时间】:2023-04-03 02:11:01
【问题描述】:

当我点击表格视图的单元格时,它们会变暗为灰色,并且在我点击不同的单元格之前不会变回白色。我必须设置某种布尔值才能让它不这样做吗?

这是解释我的问题的屏幕截图:

如果它意味着更详细的描述,则指向其他网站的链接会有所帮助。 (除非它是一个超级简单的修复程序,否则正确的代码或采取的步骤将比链接更容易。)

【问题讨论】:

  • 那么,当你按下一个单元格时,你只想要复选标记而不是灰色选择?

标签: ios swift uitableview


【解决方案1】:

这是 UITableView 的默认行为。

您必须在 UITableViewController 类中的 didSelectRowAtIndexPath 内调用 deselectRowAtIndexPath

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    tableView.deselectRowAtIndexPath(indexPath, animated: true)
}

查看 iOS 文档了解更多信息。

UITableView
UITableViewDelegate

【讨论】:

  • 抱歉回复晚了。这正是我想要的。谢谢,卡西姆!
  • 考虑@Anthony Dito 的回答。这样可以避免在点击时出现灰色闪烁的行。
  • 正确的解决方案是:cell.selectionStyle = .none in cellForRowAt 方法
【解决方案2】:

斯威夫特 3

选项 1:(我一直使用)

要在选择灰色后给它淡出动画,您可以这样做:

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

选项 2:

要完全消除高亮效果,您可以将此行添加到您的 cellForRowAt

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

【讨论】:

  • 您的选项 2 帮助我解决了在设置颜色之前闪烁灰色背景效果的问题。非常感谢!
  • @user30646 很高兴它帮助了你:)
【解决方案3】:

你可以通过几种方式做到这一点......

  1. tableView.allowsSelection = false

  2. 您可以将xCode Storyboard中的tableView设置为在第四个选项卡下没有任何选择。

  3. 或者,您可以在单元格cell.selectionStyle = UITableViewCellSelectionStyle.None上执行此操作

你想要的最终将是你所追求的行为。只需做一些实验。

【讨论】:

    【解决方案4】:

    斯威夫特 3

    在自定义单元格中添加:

     override func awakeFromNib() {
        super.awakeFromNib()
        selectionStyle = .none
    }
    

    这样可以确保在点击单元格时您甚至不会看到灰色。 UITableViewDelegate 中的此代码仅在点击时取消选择。

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

    【讨论】:

      【解决方案5】:

      只需单击单元格并转到属性检查器,您将找到选择样式,选择无。

      【讨论】:

        【解决方案6】:

        您可以通过以下方式更改样式:

         [cell setSelectionStyle:UITableViewCellSelectionStyleGray];
        

        【讨论】:

          【解决方案7】:

          斯威夫特 4.1

          tableView.deselectRow(at: indexPath, animated: true)
          

          【讨论】:

            【解决方案8】:

            在故事板或 xib 中, 在表格视图单元格中, 您可以选择选择“无”, 在属性检查器中

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2021-10-11
              • 2014-05-16
              • 1970-01-01
              • 1970-01-01
              • 2022-01-03
              • 2021-11-10
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多