gpt4 book ai didi

ios - UITableViewCell 在点击时突出显示

转载 作者:可可西里 更新时间:2023-11-01 00:42:17 26 4
gpt4 key购买 nike

我的 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
}
}

最佳答案

您可以将第一节中所有UITableViewCellsselectionStyle设置为.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
}
}

关于ios - UITableViewCell 在点击时突出显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42284918/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com