UIControl无法识别表格视图单元格内的点击,已从iOS 14停止工作

UIControl无法识别表格视图单元格内的点击,已从iOS 14停止工作,ios,swift,Ios,Swift,在iOS 14发布之前,我启动了一个应用程序。我停止使用Swift几个月了,现在,模拟器正在运行iOS 14,从那时起,我遇到了一个问题,我在tableView单元格中的UIControl没有注册点击 这是我的TableView(普通、非定制或其他): 以下是我尝试设置轻触手势的方式: extension TodoListVC: UITableViewDelegate, UITableViewDataSource { func tableView(_ tableView: UITableVie

在iOS 14发布之前,我启动了一个应用程序。我停止使用Swift几个月了,现在,模拟器正在运行iOS 14,从那时起,我遇到了一个问题,我在tableView单元格中的UIControl没有注册点击

这是我的TableView(普通、非定制或其他):

以下是我尝试设置轻触手势的方式:

extension TodoListVC: UITableViewDelegate, UITableViewDataSource {

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: todoCellIdentifier, for: indexPath) as! TodoCell
    
    let todo = todoListViewModel.todos[indexPath.row]
    cell.model = todo
    cell.selectionStyle = .none
    
    
    let checkBox = cell.checkbox
    checkBox.index = indexPath.row
    checkBox.addTarget(self, action: #selector(onCheckBoxValueChange(_:)), for: .valueChanged) // touchUpInside also does not work.
    
    
    return cell
}

@objc func onCheckBoxValueChange(_ sender: UICheckBox) {
    var todo = todoListViewModel.todos[sender.index]
    todo.isDone = sender.isChecked
    tableView?.reloadRows(at: [IndexPath(row: sender.index, section: 0)], with: .none)
    todoListView.reloadProgress()
}
}

复选框是我从教程中复制的自定义UIControl,在iOS 14之前它工作得很好,现在我想不出它有什么问题。此处添加的文件相当大,但如果有必要,我可以提供代码。

转到您的
TodoCell
并将其添加到您的
初始值设定项中:

 override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
    
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    contentView.isUserInteractionEnabled = true

}
它将重新开始工作。如果未按编程方式创建单元格,则可以执行以下操作:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: todoCellIdentifier, for: indexPath) as! TodoCell

let todo = todoListViewModel.todos[indexPath.row]
cell.model = todo
cell.selectionStyle = .none
cell.contentView.isUserInteractionEnabled = true

let checkBox = cell.checkbox
checkBox.index = indexPath.row
checkBox.addTarget(self, action: #selector(onCheckBoxValueChange(_:)), for: .valueChanged) // touchUpInside also does not work.


return cell
}

要进行调试,您可以打印
contentView.isUserInteractionEnabled的值,它将为您提供布尔值并使用它可以查看问题是否存在。最有可能返回的是
false
,上述解决方案在iOS 14和Xcode 12上运行良好。

转到
处理
,并将其添加到
初始值设定项中:

 override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
    
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    contentView.isUserInteractionEnabled = true

}
它将重新开始工作。如果未按编程方式创建单元格,则可以执行以下操作:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: todoCellIdentifier, for: indexPath) as! TodoCell

let todo = todoListViewModel.todos[indexPath.row]
cell.model = todo
cell.selectionStyle = .none
cell.contentView.isUserInteractionEnabled = true

let checkBox = cell.checkbox
checkBox.index = indexPath.row
checkBox.addTarget(self, action: #selector(onCheckBoxValueChange(_:)), for: .valueChanged) // touchUpInside also does not work.


return cell
}

要进行调试,您可以打印
contentView.isUserInteractionEnabled的值,它将为您提供布尔值并使用它可以查看问题是否存在。很可能它会返回
false
,上述解决方案在iOS 14和Xcode 12上运行良好。

非常感谢!这又起作用了。你能告诉我你是怎么知道的吗?我找了,但没找到。只是为了让我知道下次去哪里找。谢谢我也像你一样陷入困境,然后我开始调试,发现这是问题/变化。请将其标记为正确答案,以便所有面临此问题的人都能看到此解决方案有效。明白了,谢谢!我将此标记为正确答案(必须等待几分钟才能实现)@Rob Dude你真是个天才!非常感谢。非常感谢,一切如愿。同样的问题,iOS 14>手机点击不会触发任何东西。这就解决了。非常感谢!这又起作用了。你能告诉我你是怎么知道的吗?我找了,但没找到。只是为了让我知道下次去哪里找。谢谢我也像你一样陷入困境,然后我开始调试,发现这是问题/变化。请将其标记为正确答案,以便所有面临此问题的人都能看到此解决方案有效。明白了,谢谢!我将此标记为正确答案(必须等待几分钟才能实现)@Rob Dude你真是个天才!非常感谢。非常感谢,一切如愿。同样的问题,iOS 14>手机点击不会触发任何东西。这就解决了问题。