0
let x = self.tableProduct.count
self.tableProduct = products
self.tableView.beginUpdates()
var insertedIndexPaths = [IndexPath]()
for i in x..<self.tableProduct.count {
    print("No : \(i)")
    insertedIndexPaths.append(IndexPath(row: i, section: 0))
}
print("Count = \(self.tableProduct.count)")
print(insertedIndexPaths)
self.tableView.insertRows(at: insertedIndexPaths, with: .none)
self.tableView.endUpdates()

Here x is the current array count. Since I'm fetching in background after assigning I update array in second line. Then I start updating table view. Lets say initially my table has 50 records that is x, then I want to add row from 51 to self.tableProduct.count which is 100. In for loop I add index path my first index path is [0,51] and last index path looks like [0,99]. Then I start to insert my new rows, here it throws

Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 17 beyond bounds [0 .. 16]'

error.When I add breakpoint to error it shows error in tableView.endUpdates() line.Here what I'm doing wrong? How can I solve this error? Thanks in advance.

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
e.k
  • 1,333
  • 1
  • 17
  • 36

0 Answers0