I am working with a UITableView and for each of the objects in the array that is the datasource for the UITableView, I am deleting them if they meet a certain if statement. My problem is that it only deletes every-other object from the array.
Code:
UIImage *isCkDone = [UIImage imageNamed:@"UITableViewCellCheckmarkDone"];
int c = (tasks.count);
for (int i=0;i<c;++i) {
NSIndexPath *tmpPath = [NSIndexPath indexPathForItem:i inSection:0];
UITableViewCell * cell = [taskManager cellForRowAtIndexPath:tmpPath];
if (cell.imageView.image == isCkDone) {
[tasks removeObjectAtIndex:i];
[taskManager deleteRowsAtIndexPaths:@[tmpPath]
withRowAnimation:UITableViewRowAnimationLeft];
}
}
What is wrong with this?
You must run your loop backwards, i.e.
If you are running it the other way round, removing an object at index position
imoves the objects in the array that are behindione position forward. In the end you will even run over the bounds of your array.