I'm trying to delete the rows that don't meet the criteria in the for loop. However, I'm getting and error that says: 'attempt to delete row 0 from section 1, but there are only 1 sections before the update." I'v never seen this before and not sure why I am getting it.
My code:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = theTableView.dequeueReusableCellWithIdentifier("MyCell") as! TableViewCell
cell.titleLabel.text = titles[indexPath.row]
cell.priceLabel.text = prices[indexPath.row]
cell.descLabel.text = descs[indexPath.row]
cell.itemImage.image = itemImages[indexPath.row]
cell.userNumber = phoneNumbers[indexPath.row] as! String
cell.timeLabel.text = datesHours[indexPath.row]! + "hr"
cell.distanceLabel.text = String(locations[indexPath.row]!) + "mi"
cell.viewController = self
self.theTableView.beginUpdates()
for (index, number) in self.locations {
if number <= 5 {
let indexPath = NSIndexPath(forRow: number, inSection: 1)
self.theTableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
}
}
self.theTableView.endUpdates()
reloadRowsAtIndexPaths
inside yourcellForRowAtIndexPath
?? It's not a good ideafor
loop in the handler for it. The only job of cellForRowAtIndexPath is to compute a cell that is known to be in the table. Nothing else.