toobiz
toobiz

Reputation: 263

Delete rows method in commitEditingStyle

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

Answers (2)

Mochi
Mochi

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

rmaddy
rmaddy

Reputation: 318874

  1. Do not call reloadData in addition to deleteRowsAtIndexPaths:. Just do one.
  2. You first need to update the data model used by your data source. In this case you need to remove the corresponding row from the self.notifications array. This means that you need a mutable array.
  3. You don't need the calls to beginUpdate/endUpdate since you are only making a single call to modify the table view.

Upvotes: 1

Related Questions