我的CellForRowAtIndexPath发送错误的信息?

时间:2014-03-17 22:17:26

标签: ios uitableview

当我调用cellForRowAtIndexPath的索引路径时:为了删除单元格,它会在选定的单元格之前删除de cell ...下面是它的工作原理:

我将信息添加到tableview:

- (BOOL)textFieldShouldReturn:(UITextField *)textField{

    [self.tasks insertObject:textField.text atIndex:0];
    [self.userdefaults setObject:self.tasks forKey:@"tasks"];
    [self.userdefaults synchronize];

    NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:(0) inSection:0];
    [self.tableview beginUpdates];
    [self.tableview insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationLeft];
    [self.userdefaults synchronize];
    [self.tableview endUpdates];
}

然后我使用CocoaControl(MCSwipeTableViewCell)中的一些手势编辑信息:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//This is for deleting a cell
[cell setSwipeGestureWithView:cross color:redColor mode:MCSwipeTableViewCellModeSwitch state:MCSwipeTableViewCellState2 completionBlock:^(MCSwipeTableViewCell *cell, MCSwipeTableViewCellState state, MCSwipeTableViewCellMode mode) {


        [tableView beginUpdates];

        I tried to use this index and didnt work-->//NSIndexPath *myindex = [self.tableview indexPathForSelectedRow];

        [self.tasks removeObjectAtIndex:[self keyForIndexPath:indexPath].row];
        [self.userdefaults setObject:self.tasks forKey:@"tasks"];
        [self.userdefaults synchronize];

        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
        [tableView endUpdates];

        NSLog(@"Did swipe \"Cross\" cell");
    }];


}

- (NSIndexPath *)keyForIndexPath:(NSIndexPath *)indexPath
{
    if ([indexPath class] == [NSIndexPath class]) {
        return indexPath;
    }
    return [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section];
}

所以当我尝试删除它时,它会在选择一个单元格之前删除它,我需要一些帮助,谢谢!

2 个答案:

答案 0 :(得分:0)

您可以在完成块中使用[tableView indexPathForCell:cell]来获取相应的indexPath。

答案 1 :(得分:-2)

尝试使用:[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];代替?