ios 14 带有 textField 的 Tableview 单元格不起作用

Isl*_*mov 4 uitableview uitextfield uikit ios14

在带有 textField 的 iOS 14 TableView 中不起作用。有人有解决方案吗?

这是示例代码:

class TestController: UIViewController {
    let tableView: UITableView = {
        let tableView = UITableView(frame: .zero, style: .grouped)
        return tableView
    }()
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .green
        view.addSubview(tableView)
        tableView.edgesToSuperview()
        tableView.backgroundColor = .gray
        tableView.register(TestCell.self, forCellReuseIdentifier: TestCell.reuseID)
        tableView.dataSource = self
    }
}
extension TestController: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: TestCell.reuseID, for: indexPath)
        return cell
    }
}

class TestCell: UITableViewCell {
    let textField = UITextField()
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        backgroundColor = .red
        addSubview(textField)
        textField.height(50)
        textField.backgroundColor = .blue
        textField.edgesToSuperview()
        selectionStyle = .none
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
Run Code Online (Sandbox Code Playgroud)

相同的代码适用于 iOS 13,但不适用于 iOS 14。有人解决了这个问题吗?(Xcode 版本 12.0 (12A7209))

Kew*_*ier 14

您不应直接在单元格中使用 addSubview,而是将其添加到 ContentView 中:

contentView.addSubview(textField)
Run Code Online (Sandbox Code Playgroud)

这应该可以解决您的问题