0

I have a UITableView that has about 30 items. Here's an overview of how my tableview works: In order to enable infinite scrolling and a small memory size, I calculate the scrolling direction, speed, and I remove the top 10 items and add 10 items to the bottom (the same goes for scrolling up the other way). If I were to scroll to an item, the indexPath.row stays at that same item when I attempt to reloadData() with a new set of 30 items.

I have tried creating my own index to use, but it has a lot facets to it that would make it pretty complicated. I'm looking to see if I can just reset indexPath.row.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    item = items[indexPath.row]
}

Sometimes, I would like to jump to another set of items that is a few pages down. So, I then replace the current array of items with a new 30 items. However, when I have already scrolled down a little (let's say I'm on the 15th cell), and I then jump to a new set of 30 items, the very first cell to show in tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell is the 15th cell. indexPath.row = 15... Why is indexPath.row not updating to 0? Is there a way to reset indexPath.row to 0 when I fill my array with a new set of items and call reloadData()?

  • *You* cannot change the `indexPath`, you can only change the cell it refers to. E.g. you can **always** return the same content for every cell, or have an array of 15 values and always access the `indexPath.row % 15`th element. – luk2302 Jul 23 '19 at 21:15
  • I see. Do you know when, if at all, the indexPath is reset to 0? – Robert Pelican Jul 23 '19 at 21:34

1 Answers1

0

Apparently, manually setting the indexPath is not possible. Instead, I just scroll the tableView to the top to ensure that it starts at the very first element.

tableView.scroll(to: .top, animated: false)