indexPath not updated after using deleteRowsAtIndexPaths
What you're doing is basically correct, but you'll need to reload the tableView
somehow. If you don't want to reload the whole tableView
, use this, which gives you an animation
as well -
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:sectionNum];
[self.o_table reloadSections:indexSet
withRowAnimation:UITableViewRowAnimationFade];
Swift 2.2
The best solution I found is to set an NSTimer
and call the reload data 0.5 sec after, which gives enough time for the animation to finish. The Selector("loadData")
is a method that contains tableview.reloadData()
_ = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: Selector("loadData"), userInfo: nil, repeats: false)
Swift 3.0
_ = Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(ViewController.loadData), userInfo: nil, repeats: false)
The reloadSections:
method works but it doesn't give you the normal animation for deleting a row, instead using a fade animation. In iOS 11 a new method is available which solves this:
[tableView performBatchUpdates:^(
{
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
completion:^(BOOL finished)
{
if( finished){
[tableView reloadData];
}
}];