ios - TableView 在删除时不选择新行

标签 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];
        }

    }   

}

最佳答案

至少,选择部分需要位于更新 block 之外!将其放在 endUpdates 调用之后。

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

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

关于ios - TableView 在删除时不选择新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15589852/

相关文章:

ios - 如何在 Alamofire 中禁用缓存

ios - 删除行 UITableView 索引问题

ios - 如何在屏幕外的UITableViewCell中获取UILabel的高度?

ios - 清除节标题上方的 UITableViewCells

ios:在点击 collectionview 单元格或 tableview 单元格时禁用第二个触摸事件

ios - 存储设置 ios5

ios - 表有时加载有时崩溃

ios - 带有 AutoLayout 的 UITableViewCell - contentView 宽度约束失败

ios - 如何设置我的 iOS Storyboard以像 Instagram Stream 一样弹跳?

ios - 当单元格移出屏幕时如何停止单元格中的 AVPlayer 播放