简体   繁体   English

TableView在删除时不选择新行

[英]TableView not selecting new row on deletion

When I delete a row in my tableView I want the row above it to be selected. 当我在tableView中删除一行时,我希望它上方的行被选中。 This doesn't happen... 这不会发生...

Here is the code: 这是代码:

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

    }   

}

At the very least, the selection part needs to be outside the updates block! 至少,选择部分需要在更新块之外 Put it after your endUpdates call. 将其放在endUpdates调用之后。

If that doesn't work, go even further: Try doing the selection part using delayed performance (eg dispatch_after ). 如果那不起作用,请进一步:尝试使用延迟的性能(例如, 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. 现在,您正在尝试执行选择,而我们仍在响应commitEditingStyle我们甚至还没有删除该行。 Delayed performance lets the interface settle down after the deletion. 延迟的性能使接口在删除后稳定下来。

You will need to use index numbering corresponding to the situation after the deletion. 删除 ,您将需要使用与情况对应的索引编号。

// 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];

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

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