I'm implementing "swipe to delete" to a TableView with notifications from API. I created a method that deletes a notification when I hard-code its notification id (which is an array). The problem is I can't figure out how to get the exact notification id to delete.
There are TableView Delegate and TableView Data Source methods that somehow get the notification id, so I suppose I should be able to get it for the purpose of my method, but I've run out of ideas.
Here's my API source code:
desc 'delete notifications'
params do
requires :notification_ids, type: Array
end
delete 'notifications/remove', root: :notifications, each_serializer: NotificationSerializer do
require_authentication!
NotificationLogic.delete_notifications params[:notification_ids], current_user
current_user.notifications
end
Here's the method for deleting notifications:
-(void)deleteNotificationWithId:(NSArray*)ids withCompletionHandler:(DeleteNotificationCompletionHandler)handler
{
NSDictionary* params = @{ @"notification_ids" : ids };
__weak typeof(self) weakSelf = self;
ReadNotificationRequest* req = [ReadNotificationRequest new];
req.notificationIds = ids;
[_objectManager deleteObject:nil
path:@"user/notifications/remove"
parameters:params
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
_secondTry = NO;
NSArray* arr = mappingResult.array;
[self notififyAboutNotifications:arr];
handler(YES, arr, nil);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
if (operation.HTTPRequestOperation.response.statusCode == 401 && !_secondTry)
{
[weakSelf relogin:^{
[weakSelf deleteNotificationWithId:ids withCompletionHandler:handler];
}];
return;
}
handler(NO, nil, error);
}];
}
and implementation of the method in NotificationTableView. It works, but I hard-code the array with number:
-(void)setNotifications:(NSMutableArray *)notifications{
_notifications = notifications;
[self reloadData];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
//Remove item in array
[self.notifications removeObjectAtIndex:indexPath.row];
// Also remove that row from the table view with an animation
[tableView deleteRowsAtIndexPaths:@[indexPath]
withRowAnimation:UITableViewRowAnimationFade];
//Remove hard-coded notification from server
[[Api sharedInstance]deleteNotificationWithId:@[@756]
withCompletionHandler:^(BOOL succes, Message *response, NSError *error) {
if(succes){
} else {
[Utils alert:error.pop_message];
}
}];}
}
#pragma mark TableView Data Source
- (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;
}
#pragma mark - UITableViewDelegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
Notification* not = self.notifications[indexPath.row];
[self.notificationDelegate notificationTapped:not];
}
This code
//Remove item in array
[self.notifications removeObjectAtIndex:indexPath.row];
Deletes the information you need, just before you need it. Instead of deleting it, read the ID out first, then delete it and use the ID.