Reputation: 263
I'd like to implement a "swipe to delete" option to a tableview. I added the "commitEditingStyle" method and it triggers the swipe and a red box with "delete" shows, but I can't work out what should be inside that method. Any help? Here's the code:
#import "NotificationTableView.h"
#import "NotificationTableViewCell.h"
@interface NotificationTableView () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) NSMutableDictionary* heights;
@end
@implementation NotificationTableView
-(void)awakeFromNib{
UINib *nib = [UINib nibWithNibName:@"NotificationTableViewCell" bundle:nil];
[self registerNib:nib forCellReuseIdentifier:@"NotificationTableViewCell"];
[self registerNib:nib forCellReuseIdentifier:@"NotificationTableViewCellSize"];
self.dataSource = self;
self.delegate = self;
self.estimatedRowHeight = 86;
self.rowHeight =
// (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1) ? UITableViewAutomaticDimension :
86;
self.tableFooterView = [UIView new];
_heights = [NSMutableDictionary new];
}
-(void)setNotifications:(NSArray *)notifications{
_notifications = notifications;
[self reloadData];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
[self reloadData];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section
{
return self.notifications.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NotificationTableViewCell* cell = [self dequeueReusableCellWithIdentifier:@"NotificationTableViewCell"];
[cell configureCellWithNotification:self.notifications[indexPath.row]];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
Notification* not = self.notifications[indexPath.row];
[self.notificationDelegate notificationTapped:not];
}
@end
Upvotes: 0
Views: 807
Reputation: 1117
Try this:
- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
//Get the notification here
Notification* not = self.notifications[indexPath.row];
//Remove item in array
[self.notifications removeObjectIdenticalTo:not];
// Also remove that row from the table view with an animation
[tableView deleteRowsAtIndexPaths:@[indexPath]
withRowAnimation:UITableViewRowAnimationFade];
}
Upvotes: 0
Reputation: 318874
reloadData
in addition to deleteRowsAtIndexPaths:
. Just do one.self.notifications
array. This means that you need a mutable array.beginUpdate
/endUpdate
since you are only making a single call to modify the table view.Upvotes: 1