繁体   English   中英

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

[英]How to delete cell with parse data in Xcode 5?

嗨,我正在使用parse为我的应用程序存储数据,并希望能够在向左滑动单元格时将其删除,尽管当我单击Delete时,我必须手动下拉并刷新以使数据消失。

无论如何,只要在滑动单元格后单击“删除”,就可以使其动画并消失吗?

这是我用来删除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];



    }];
    }
}

提前致谢!

您甚至在不知道数据是否已成功删除之前就调用了该方法。

- (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. 更新后端服务器(解析)模型=删除单元数据
  2. 更新客户端模型(在本例中为self.objects
  3. 相应地对单元进行动画处理

尝试这个:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM