自从更新到 xcode 12 以来,我无法在 UITableViewCell 中放置任何 UIControl

Jas*_*ter 8 uitableview uicontrol xcode12

我有一个使用 tableview 的搜索表单。今天更新 Xcode 12 后,UISwitch、UITextField、UISlider 在嵌套在 UITableViewCell 中时不再工作。是否有一个属性发生了变化,我需要设置它才能再次工作?

为了确保这不仅仅是我的项目,我创建了一个新项目并在其中放置了一个 UITextField,但它也不起作用。

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell()
    
    let textField = UITextField(frame: CGRect(x: 5, y: 5, width: 400.0, height: 25.0))
    textField.delegate = self
    textField.backgroundColor = .blue
    cell.addSubview(textField)

    return cell
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print("this will get called even when selecting the UITextField")
}

func textFieldDidBeginEditing(_ textField: UITextField) {
    print("this is never called")
}
Run Code Online (Sandbox Code Playgroud)

mat*_*att 20

你的代码总是错误的:

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

您绝不能向单元格添加子视图。将子视图添加到单元格的contentView.