自从更新到 xcode 12 以来,我无法在 UITableViewCell 中放置任何 UIControl [英] Since Updating to xcode 12 I am not able to place any UIControl inside UITableViewCell

查看:27
本文介绍了自从更新到 xcode 12 以来,我无法在 UITableViewCell 中放置任何 UIControl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a search form that uses a tableview. After updating Xcode 12 today the UISwitch, UITextField, UISlider no longer work when nested inside a UITableViewCell. Is there a property that has changed that I need to set to make this work again?

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

To be sure it wasn't just my project, I created a new project and nestled a UITextField inside of it and it doesn't work either.

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

推荐答案

你的代码总是错的:

cell.addSubview(textField)

您必须永远向单元格添加子视图.将子视图添加到单元格的 contentView.

You must never add a subview to a cell. Add the subview to the cell's contentView.

这篇关于自从更新到 xcode 12 以来,我无法在 UITableViewCell 中放置任何 UIControl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆