TableView 在删除时不选择新行 [英] TableView not selecting new row on deletion

查看:22
本文介绍了TableView 在删除时不选择新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When I delete a row in my tableView I want the row above it to be selected. This doesn't happen...

代码如下:

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

    }   

}

推荐答案

至少,选择部分需要在更新块之外!把它放在你的 endUpdates 调用之后.

At the very least, the selection part needs to be outside the updates block! Put it after your endUpdates call.

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

If that doesn't work, go even further: Try doing the selection part using delayed performance (e.g. dispatch_after). Right now you're trying to perform the selection while we're still in the middle of responding to commitEditingStyle - we haven't even deleted the row yet. Delayed performance lets the interface settle down after the deletion.

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

You will need to use index numbering corresponding to the situation after the deletion.

这篇关于TableView 在删除时不选择新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆