UITableView with UIButton or UITextField on iOS 14.2 does not work anymore

Hello,
does anyone also have the problem that when I display a row in UITableView with UITextField or UITextButton.
I can't type or click anything? i cannot select my text to input a new one....

The same code base works on 13.xx and below.

class ViewController: UIViewController, UITableViewDataSource,UITableViewDelegate, UITextFieldDelegate {
    let tableViewData = Array(repeating: "Item", count: 5)
    @IBOutlet weak var tableView: UITableView!
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.register(UITableViewCell.self,
        forCellReuseIdentifier: "TableViewCell")
        tableView.dataSource = self
tableView.delegate = self
    }

    func tableView( tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 5
    }
    func tableView(
tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell",for: indexPath)

        cell.selectionStyle = UITableViewCell.SelectionStyle.none
        let text = UITextField(frame: CGRect(x: 0, y: 0, width: 300, height: 40))
        text.text = "asdfasfasf"
text.delegate = self
        cell.addSubview(text)
        return cell
    }
}

Please HELP.... :-(
Thanks a lot
Sven
Do you set the tableView delegate somewhere ?
i have updated my example
but it still does not work
Why do you register ? Isn't the cell already defined in IB ?

But anyway, I tested your code with a subtitle cell ; I added the following in cellForRowAt function
Code Block
cell.selectionStyle = UITableViewCell.SelectionStyle.none
let text = UITextField(frame: CGRect(x: 0, y: 0, width: 300, height: 40))
text.text = "asdfasfasf"
text.delegate = self
cell.addSubview(text)

It works. TextField is editable.

I have added the registration.
TextField is still editable, but the default cell content disappears.

So, could you show the complete code of the class ?
it only works in iOS 13.6 and below in my case...... iOS 14.2 is not working :-(
i am using a UIViewController, not a UITableViewController

here my complete class:

import UIKit


Code Block
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate {
    @IBOutlet weak var tableView: UITableView!
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.register(UITableViewCell.self,forCellReuseIdentifier: "TableViewCell")
        tableView.dataSource = self
        tableView.delegate = self
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 5
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell",for: indexPath)
        cell.selectionStyle = UITableViewCell.SelectionStyle.none
        //cell.textLabel?.text = self.tableViewData[indexPath.row]
        let text = UITextField(frame: CGRect(x: 0, y: 0, width: 300, height: 40))
        text.text = "asdfasfasf"
        text.delegate = self;
        cell.addSubview(text)
        return cell
    }
}

I also tested on iOS 14.2 (simulator) with Xcode 12.2.

I tested with a tableView inside a UIViewController as well.

Could you check in code that text isUserInteractionEnabled ?
Code Block
text.text = "asdfasfasf"
print("Interaction", text.isUserInteractionEnabled)

yes it is,

Interaction true
Interaction true
Interaction true
Interaction true
Interaction true

but i cannot click it

with tab is it possible to reach the content and change it, but no per click/ touch (Simulator/ real iOS iPad with iOS 14.2)
update, i completely reinstalled xcode....

the same behavior...

but i figured out, if i scroll complete all visible rows up, after that i can select the text.....
i am complete helpless... i dont know what i can do
Please try adding one line as shown below:
Code Block
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell",for: indexPath)
cell.selectionStyle = UITableViewCell.SelectionStyle.none
//cell.textLabel?.text = self.tableViewData[indexPath.row]
let text = UITextField(frame: CGRect(x: 0, y: 0, width: 300, height: 40))
text.text = "asdfasfasf"
text.delegate = self;
cell.addSubview(text)
cell.contentView.isUserInteractionEnabled = false //<-
return cell
}

The same issue was discussed in the dev forums recently.


By the way, your code is still wrong even if it seemingly worked in older iOS's.
Please remember that cells may be reused. With calling addSubview(text) in tableView(_:cellForRowAt:), the cell may have multiple text fields.
In some context, for example the table view has a small fixed number of cells and may never scroll, the cells may not be reused.
But even in such cases, you should better avoid this sort of wrong coding.
Hi,

i come from the Xamarin world and just tried to find out where the problem is.
if i had something wrong in my code, sorry.

meanwhile, i found the problem. i have to add my controls to the contenview and not to the cell.

this is the solution.
thank you very much and merry christmas
Sven
I don't understand.

Adding to the cell or adding to the contentView is a totally different use case.
In the former, button action may be different for each cell ; in the latter it is not related to the cell.

Your initial post showed you wanted the first. Did you change your spec ?
UITableView with UIButton or UITextField on iOS 14.2 does not work anymore
 
 
Q