Reputation: 352
I created a method which updates my UITableView when the user wants to delete a cell.
-(void)removeMoreFriendsRow:(NSNotification *)notification {
NSDictionary *d = [notification userInfo];
NSIndexPath *index = [d objectForKey:@"indexPath"];
[self.p_currentFriends removeObjectAtIndex:index.row];
[self.o_table deleteRowsAtIndexPaths:[NSArray arrayWithObject:index] withRowAnimation:UITableViewRowAnimationRight];
}
p_currentFriends is a NSMutableArray which contains all the objects printed by the UITableView.
This method works fine when I use it once, but it fails when I use it more than once.
It appears that the indexPath stay the same at each call of deleteRowsAtIndexPaths.
For example,
The only way I found to make it works is to use reloadData but I want to have a animation on the cell deletion.
How can I do this?
Upvotes: 10
Views: 3329
Reputation: 300
With the following code, I am able to do it correctly but I have to manage it separately for versions before iOS 11 and from iOS 11.
See following example:
if (@available(iOS 11.0, *)) { // indexPath is your needed indexPath
[self->chatTableVC.messagesTable performBatchUpdates:^{
[self->chatTableVC.messagesTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
} completion:^(BOOL finished) {
[self->chatTableVC.messagesTable reloadData];
}];
} else {
[self->chatTableVC.messagesTable beginUpdates];
[self->chatTableVC.messagesTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[self->chatTableVC.messagesTable endUpdates];
}
Upvotes: 0
Reputation: 11
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];
}
}];
Upvotes: 1
Reputation: 6526
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)
Upvotes: 5
Reputation: 1
Refer to the above answer,Here is my solution:
- (void)deleteRowAtIndex:(NSIndexPath *)indexPath{
[self.dataSource.datas removeObjectAtIndex:indexPath.row];
[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationBottom];
[self.tableView endUpdates];
// NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:indexPath.section];
// [self.tableView reloadSections:indexSet
// withRowAnimation:UITableViewRowAnimationNone];
// [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop];
NSLog(@"INDEXPATH:%@",indexPath);
[NSTimer scheduledTimerWithTimeInterval:0.5 target:self.tableView selector:@selector(reloadData) userInfo:nil repeats:NO];
}
Upvotes: 0
Reputation: 4164
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];
Upvotes: 9
Reputation: 882
You should surround delteRowsAtIndexPaths: and other table editing operations with beginUpdates and endUpdates. That may resolve your issue.
Upvotes: 0