I'm trying to insert a new row, and immediately make it the first responder.
Using .insertRows()
seems to execute asynchronously, even with .none
. for the animation. That means that when I call cellForRow(at:)
, it returns nil.
What would be the best way to wait for the insert to finish, before calling becomeFirstResponder
?
This does not work:
self.tableView.insertRows(at: [newIndex], with: .none)
self.tableView.cellForRow(at: newIndex)?.becomeFirstResponder() //Returns nil
CodePudding user response:
Why do you have to use .insertRaws()? I mean, why don't you just call reloadData() after adding new item to array?
self.array.append(newData) // Add item to data source
self.tableView.reloadData() // I am not sure if this is required.
let newIndex = IndexPath(row: self.array.count(), section: 0)
self.tableView.cellForRow(at: newIndex)?.becomeFirstResponder()
CodePudding user response:
Put the insertRows(at:with:)
call between a beginUpdates()
and endUpdates()
calls:
self.tableView.beginUpdates()
self.tableView.insertRows(at: [newIndex], with: .none)
self.tableView.endUpdates()
self.tableView.cellForRow(at: newIndex)?.becomeFirstResponder()
Anyway I'm not so fond of letting as first responder a cell in this way: what if it's not visible?