0

I'm trying to do an infinite scroll table view, but the problem is that when I try to reload the rows I'm getting "attempt to delete row 12 from section 0 which only contains 10 rows before the update".

I load the first 10 results, and after they are displayed I load the rest. But before the crash I po'd listObj.count just to be sure it was correctly appending the results and yes, it works.

private func loadDataBackground() {
        if self.totalPages! > self.currentPage {
            var response: [AnyHashable]?
            self.currentPage += 1
            self.api = Api() 

            if self.type == "sometype" {
                self.api.someMethodPaginated(page: self.currentPage, resultsbyPage: self.resultsByPage!)
                response = Api.responseToArray(self.api) as? [AnyHashable]
            }

            if response != nil {
                for object in response! {
                    self.listObj.append(object)
                }
                let indexToReload = calculateIndexPathsToReload(newListaObj: response!)

                DispatchQueue.main.async {
                    self.myTableView.beginUpdates()
                    self.myTableView.reloadRows(at: indexToReload, with: .automatic)
                    self.myTableView.endUpdates()
                }
            }
        }
    }
private func calculateIndexPathsToReload(newListObj : Array<Any>) -> [IndexPath] { //comments
      let startIndex = listObj.count - newListObj.count
      let endIndex = startIndex + newListObj.count
      return (startIndex..<endIndex).map { IndexPath(row: $0, section: 0) }
    }

I used the second method described here to detect the last row.

What am I doing wrong?

Jalil
  • 1,167
  • 11
  • 34

0 Answers0