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

46scxncf  于 2023-08-08  发布在  iOS
关注(0)|答案(2)|浏览(118)

我有一个应用程序,在iOS 14出来之前。我停止使用Swift几个月,现在,模拟器正在运行iOS 14,从那时起,我遇到了一个问题,我在tableView单元格中的UIControl不注册抽头。
这是我的TableView(普通的,没有自定义或任何东西):

private let tableView: UITableView = {
    let table = UITableView(frame: .zero, style: .plain)
    table.translatesAutoresizingMaskIntoConstraints = false
    table.backgroundColor = .white
    table.separatorInset.right = table.separatorInset.left
    
    
    return table
}()

字符串
这是我尝试设置点击手势的方式:

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之前它工作得很好,现在我不知道它出了什么问题。文件是相当大的添加在这里,但如果这是必要的,我可以提供代码。

7gyucuyw

7gyucuyw1#

转到你的TodoCell,并在你的initializer中添加这个:

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上运行良好。

rkttyhzu

rkttyhzu2#

此外,使用的UIControl中每个subviewisUserInteractionEnabled必须设置为false。

subview.isUserInteractionEnabled = false

字符串

相关问题