【问题标题】:UITableViewCell getting highlighted when tapped点击时 UITableViewCell 突出显示
【发布时间】:2017-07-06 05:04:50
【问题描述】:

我的 VC 中有一个 UITableView,基本上我想要的是它的第一部分是不可点击的。但我不能使用isUserInteractionEnabled 因为我在本节的每一行中都有 UISwitch 。将selectionStyle 设置为.none 没有任何改变。我只能在界面检查器中选择No Selection 来禁用这些行,但它会禁用整个表。我该怎么办?

编辑

这是我的自定义单元格类

class CustomCell: UITableViewCell {
    override func setHighlighted(_ highlighted: Bool, animated: Bool) {

        if highlighted {
            self.backgroundColor = ColorConstants.onTapColor
        } else {
            self.backgroundColor = .clear
        }
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        if selected {
            self.backgroundColor = ColorConstants.onTapColor
        } else {
            self.backgroundColor = .clear
        }
    }

}

【问题讨论】:

标签: ios swift xcode uitableview


【解决方案1】:

您可以将第一部分中所有UITableViewCells中的selectionStyle设置为.none,如下所示:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "YOURIDENTIFIER")

    if indexPath.section == 0 {
        cell.selectionStyle = .none
    } else {
        cell.selectionStyle = .default
    }

    return cell
}

然后在你的didSelectRowAtIndexPath()方法中你可以检查if (indexPath.section != YOURSECTION)

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if indexPath.section == 0 {
        // DO NITHING
    } else {
        // DO WHATEVER YOU WANT TO DO WITH THE CELLS IN YOUR OTHER SECTIONS
    }
}

【讨论】:

  • 正如我所说,它仍然会在点击时突出显示。 Mb 的原因是将 selectionStyle 设置为 none 可以避免执行 didSelectRowAt,但它仍然调用 willSelectRowAt
  • 我找到了原因,这对我来说很愚蠢。我忘记了,我在自定义 UITableViewCell 子类中覆盖了 setHighlighted 函数。我想改变点击单元格的颜色。我应该怎么做而不是我做了什么?答案已更新
【解决方案2】:

您必须在代码中为每个单元格设置选择样式。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = table.dequeue...

    if indexPath.section == 0 {
        cell.selectionStyle = .none
    } else {
        cell.selectionStyle = .default
    }

    return cell
}

【讨论】:

    【解决方案3】:

    在 cellForRowAt 添加 cell.selectionStyle = .none

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
         if(indexPath.section == desiredSection){
            cell.selectionStyle = .none
            return cell;
         }
    

    【讨论】:

      【解决方案4】:

      所以,我找到了将selectionStyle 设置为.none 的单元格在点击时突出显示的原因。因为我覆盖了UITableViewCellsetHighlighted 方法(如问题所示),所以我添加了 shouldHighlightRowAt 方法,如下所示:

      func tableView(_ tableView: UITableView, shouldHighlightRowAt indexPath: IndexPath) -> Bool {
          if indexPath.section == 0 {
              return false
          } else {
              return true
          }
      }
      

      感谢大家帮助我

      【讨论】:

        猜你喜欢
        • 2014-03-10
        • 2017-05-20
        • 1970-01-01
        • 2012-09-28
        • 2010-10-23
        • 1970-01-01
        • 1970-01-01
        • 2011-08-01
        • 1970-01-01
        相关资源
        最近更新 更多