Kirill Kirill - 4 months ago 39
iOS Question

UITableView scroll to not-existsing cell programmatically

My tableview contains 20 cells. The screen is placed only 8 cells, so this function

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell

calls 8 times and I fill only first 8 cells.

This method calls 8 times too:

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if (indexPath.row == 11)
tableView.scrollToRow(at: indexPath, at: .top, animated: true)

and I haven't indexPath.row == 11.
How can I scroll to 11th cell?


Create an index path let indexPath = NSIndexPath(forRow: 11, inSection: 0)

then pass this indexPath as self.tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Top, animated: false)

This will automatically scroll it to required cell as mentioned in indexPath.