簡體   English   中英

UIControl 無法識別表格視圖單元格內的點擊,從 iOS 14 停止工作

[英]UIControl not recognizing tap inside table view cell, stopped working from iOS 14

在 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 之前運行良好,我現在不知道它有什么問題。 該文件非常大,無法在此處添加,但如果有必要,我可以提供代碼。

轉到您的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。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM