Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Uitableview 文本字段不工作的ios 14 Tableview单元格_Uitableview_Uikit_Uitextfield_Ios14 - Fatal编程技术网

Uitableview 文本字段不工作的ios 14 Tableview单元格

Uitableview 文本字段不工作的ios 14 Tableview单元格,uitableview,uikit,uitextfield,ios14,Uitableview,Uikit,Uitextfield,Ios14,在iOS 14中,带有文本字段的TableView不工作。有人有解决办法吗 以下是示例代码: class TestController: UIViewController { let tableView: UITableView = { let tableView = UITableView(frame: .zero, style: .grouped) return tableView }() override func viewDidLo

在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")
    }
}

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

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

contentView.addSubview(textField)

这应该可以解决您的问题

在iOS 14中,UITableViewCellContentView层次结构是不同的

在tableView中(u3;:cellForRowAt:),而不是执行以下操作:

cell.addSubview(textField)
将其更改为:

 cell.contentView.addSubview(textField)