iOS: 查找要从服务器删除的项目的 NSArray

iOS: Finding the NSArray of an item to delete from server

我正在将 "swipe to delete" 实施到带有来自 API 的通知的 TableView。我创建了一个方法,当我硬编码它的通知 id(它是一个数组)时删除通知。问题是我不知道如何获取要删除的确切通知 ID。

有 TableView Delegate 和 TableView Data Source 方法可以以某种方式获取通知 ID,所以我想我应该能够为我的方法获取它,但我 运行 没有想法.

这是我的 API 源代码:

  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

删除通知的方法如下:

-(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);
                  }];
}

以及 NotificationTableView 中方法的实现。它有效,但我用数字硬编码数组:

-(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];
}

这个代码

//Remove item in array
[self.notifications removeObjectAtIndex:indexPath.row];

在您需要之前删除您需要的信息。与其删除,不如先把ID读出来,再删除再用ID。