Reputation: 4111
i have a table view.i am implementing a functionality where cells will be start fading(decrease alpha over time duration of 30 secs).After completing 30 secs, completion handler of view animation is called to delete row permanently from data source(an array).All the stuff goes in cellForRowAtIndexPath delegate method. My problem is in keeping in sync between array count before update and after update.
Code :
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *postCellId = @"postCell";
UITableViewCell *cell = nil;
NSUInteger row = [indexPath row];
cell = [tableView dequeueReusableCellWithIdentifier:postCellId];
if (cell == nil) {
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:postCellId] autorelease];
}
Post *currentPost = [posts objectAtIndex:row];
cell.textLabel.text = [currentPost postTitle];
cell.textLabel.font = [UIFont systemFontOfSize:14];
cell.detailTextLabel.text = [currentPost postDescr];
cell.detailTextLabel.font = [UIFont systemFontOfSize:10];
NSTimeInterval duration = 30;
[UIView animateWithDuration:duration delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionAllowUserInteraction
animations:^ {
cell.contentView.alpha = 0;
} completion:^(BOOL finished) {
[self.tableView beginUpdates];
NSLog(@"delete index >> %d from array >> %@", row, posts);
[posts removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
}];
return cell;
}
logs:
2015-06-04 07:22:05.764 Feeder[1177:60b] delete index >> 0 from array
( "", "", "", "", "" ) 2015-06-04 07:22:05.766 Feeder[1177:60b] delete index >> 1 from array >> ( "", "", "", "" ) 2015-06-04 07:22:05.767 Feeder[1177:60b] delete index >> 2 from array >> ( "", "", "" ) 2015-06-04 07:22:05.768 Feeder[1177:60b] delete index >> 3 from array >> ( "", "" )
if you see last log, it is the issue of crash.
crash log:
2015-06-04 07:22:05.770 Feeder[1177:60b] * Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM removeObjectAtIndex:]: index 3 beyond bounds [0 .. 1]'
how to fix it.
Upvotes: 2
Views: 775
Reputation: 10479
You can try this:
[self.tableView beginUpdates];
NSInteger postIndex = [posts indexOfObject:currentPost];
NSIndexPath *path = [NSIndexPath indexPathForRow:postIndex inSection:0];
NSLog(@"delete index >> %d from array >> %@", path.row, posts);
[posts removeObject:currentPost];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:path] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
Upvotes: 2
Reputation: 24714
Your error is you keep the old indexPath in the block,and not update it.
For example: If you have 2 records in your array, you action in complete block is
But,if you delete first cell(row 0), your row 1 indexPath.row update to 0.But you want to delete 1.
So,I think you can dynamic get the indexPath,then delete
[self.tableView beginUpdates];
NSIndexPath *path = [tableView indexPathForCell:cell];
NSLog(@"delete index >> %d from array >> %@", path.row, posts);
[posts removeObjectAtIndex:path.row];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:path] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
I am not sure if this will work,just try.
Upvotes: 2