如何在Xcode 5中删除带解析数据的单元格?

时间:2014-07-09 02:07:16

标签: ios objective-c xcode parse-platform

您好我使用parse为我的应用程序存储数据,并希望能够在向左滑动单元格时将其删除,但是当我点击删除时我必须手动下拉并刷新为了使数据消失。

在刷完单元格后,只要我点击删除,有没有制作动画并立即离开?

这是我用来删除ViewController.m中的单元格数据的代码:

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [self.tableView setEditing:editing animated:animated];
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {

    // Remove the row from data model
    PFObject *object = [self.objects objectAtIndex:indexPath.row];
    [object deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        [self refreshControl];
        [tableView reloadData];



    }];
    }
}

提前致谢!

2 个答案:

答案 0 :(得分:0)

在您知道数据是否已成功删除之前,您正在调用该方法。

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
  if (editingStyle == UITableViewCellEditingStyleDelete) { 
    //try this
    PFObject *object = [self.objects objectAtIndex:indexPath.row];
    [self.objects removeObjectAtIndex:indexPath.row];

    //found the code for removing a row. 
    [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

    // Remove the row from data model
    [object deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
      if (!succeeded){
        //we had an error
        // gives the error log and also how it relates to the user. 
        NSLog(ERROR: %@, %@", error, [error userInfo]);
      }
    }];
  }
}

编辑:尝试上面编辑的解决方案。

答案 1 :(得分:0)

要使细胞正确生成动画,您需要实现以下目标:

  1. 更新后端服务器(Parse)model =删除单元格数据
  2. 更新客户端模型(在本例中为self.objects
  3. 相应地为单元设置动画
  4. 试试这个:

    - (void)tableView:(UITableView *)tableView
        commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
        forRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if (editingStyle == UITableViewCellEditingStyleDelete) {
            // 2. Update client model
            [self.objects removeObjectAtIndex:indexPath.row];
    
            // 3. Animate cells
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                withRowAnimation:UITableViewRowAnimationFade];
    
            // 1. Update backend model
            PFObject *object = [self.objects objectAtIndex:indexPath.row]; 
            [object deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                if (error) {
                    // If the backend reports an error, you'd need to readd the cell or
                    // display an error message to the user
                }
            }];
        }
    }