Uitableview Reloaddata Automatically Calls Resignfirstresponder

UITableView reloadData automatically calls resignFirstResponder

This reads like expected behavior - the picker belongs to a particular cell, that cell gets reloaded and is not the first responder any more. I guess one had to select a specific element anyway for the picker to appear, i.e. to make it first responder.

So you either need to make it become first responder again after reloading, or update the specific cell directly.

UITableView reloadData causes UITextField to resignFirstResponder

Your text field is resigning because reloaded cells are sent a -resignFirstResponder message due to the fact that their survival is not guaranteed after a reload. See this related question for more.

TableView viewForHeaderInSection Won't Resume First Responder

Okay, these approaches are useful but I discovered here is the only answer that works:

First, resign the first responder after the textField disappears before calling reloadData()

 private var searchMode = false {
didSet {
if let tv = self.tableView.headerView(forSection: 0), let stv = tv as? SearchTableViewHeader {
stv.searchField.resignFirstResponder()
}
tableView.reloadData()
}
}

Then, set first responder at the very last tableView function. This is because there is unexpected behavior is setting first responder in viewForHeader

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
if searchMode, let stv = view as? SearchTableViewHeader {
stv.searchField.becomeFirstResponder()
}
}

UITableView dismisses keyboard on reloadData in one place, but not in another?

So, the answer turned out to be too convoluted and app-specific to be worth walking through in detail, but basically due to a bizarre and hack-y method of implementation, there were several custom objects related to the workings of the UISearchBar that were located within the cell of the first row (section 0, row 0) of the UITableView.

When the UITableView was reloaded, that first row got re-made, which meant that those objects got re-made, which dismissed the keyboard. When I asked my new boss why they'd done it like this, he couldn't remember.

Oof.

Preventing first responder to resign on tableview reload

You need to disconnect the stream that causes table view updates from the stream that changes the addresses.

And example might be something like:

typealias ID = Int // an example
typealias Address = String // probably needs to be more involved.

struct Input {
let updateAddress: Observable<(ID, Address)>
}

struct Output {
let cells: Observable<[ID]>
let addresses: Observable<[ID: Address]>
}

The table view would be bound to the cells while each cell would be bound to the addresses observable with a compactMap to extract its particular information.

That way, you can update a particular cell without reloading any of the cells in the table view.

A fully fleshed out example of this can be found in my RxMultiCounter example.

Change UITableView row height without dismissing keyboard

Yes, you can have the row heights adjust as you type. It's not documented, and it defies reason, but all you need to do is set the new value in tableView.rowHeight (or have heightForRowAtIndexPath:ready to compute the new heights), then do this:

[tableView beginUpdates]; // updates the row heights ...
[tableView endUpdates]; // ... nothing needed in between


Related Topics



Leave a reply



Submit