TableView在删除时不选择新行

时间:2013-03-23 17:29:41

标签: ios uitableview

当我在tableView中删除一行时,我希望选择它上面的行。这不会发生......

以下是代码:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        if (indexPath.section == 0) {
            Formula *toDelete = (Formula *)[self.userFRMList objectAtIndex:indexPath.row];
            NSManagedObjectContext *context = (NSManagedObjectContext *)[(PSAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
            NSError *error = nil;
            [context deleteObject:toDelete];
            [context save:&error];
            [self updateDataSource];
            [tableView beginUpdates];
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
            if ([self.userFRMList count] != 0) {

                if (indexPath.row == 0) {
                    [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0  inSection:0] animated:YES scrollPosition:UITableViewScrollPositionNone];
                    self.crtFormula = [self.userFRMList objectAtIndex:0];
                    [self.delegate theFormulaIs:self.crtFormula];

                } else {
                    [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row - 1  inSection:indexPath.section] animated:YES scrollPosition:UITableViewScrollPositionNone];
                    self.crtFormula = [self.userFRMList objectAtIndex:indexPath.row - 1];
                    [self.delegate theFormulaIs:self.crtFormula];

                }
            }else {
                self.crtFormula = nil;
                [self.delegate showBlank];
            }
            [tableView endUpdates];
        }

    }   

}

2 个答案:

答案 0 :(得分:1)

至少,选择部分需要在外面更新块!将其放在endUpdates电话之后。

如果这不起作用,请更进一步:尝试使用延迟性能(例如dispatch_after)来执行选择部分。现在你正在尝试执行选择,而我们还在回复commitEditingStyle - 我们甚至还没有删除该行。延迟性能可让界面在删除后稳定下来。

在删除后,您需要使用与情况相对应的索引编号。

答案 1 :(得分:0)

// For example I have set deletedRowIndex to 10, and next Line will select Row Number 9 in tableview
int deletedRowIndex = 10;
[tblViewList selectRowAtIndexPath:[NSIndexPath indexPathWithIndex:deletedRowIndex-1] animated:YES scrollPosition:UITableViewScrollPositionMiddle];