Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何删除节的最后一行?_Iphone_Cocoa Touch_Uikit_Uitableview - Fatal编程技术网

Iphone 如何删除节的最后一行?

Iphone 如何删除节的最后一行?,iphone,cocoa-touch,uikit,uitableview,Iphone,Cocoa Touch,Uikit,Uitableview,这个问题让我过去几个小时都很忙。我有两个部分,每个部分一行。当我删除其中一个部分中的行时,它会抛出一个异常,表示这是一个无效的更新(更新前后的行数/部分数不相同)。这是可以理解的,因为我删除了节的最后一行,因此删除了该节。问题是如何避免这种例外 我的数据源一切正常。我反复检查(相信我) 因此,正如线程标题所述,如何删除节的最后一行而不产生异常 谢谢 Bart当您删除一行时,并且该行是其节的最后一行,您还需要删除该节。基本上,您需要跟踪要删除的索引路径(与行关联)和与需要删除的节相关的索引,因为它

这个问题让我过去几个小时都很忙。我有两个部分,每个部分一行。当我删除其中一个部分中的行时,它会抛出一个异常,表示这是一个无效的更新(更新前后的行数/部分数不相同)。这是可以理解的,因为我删除了节的最后一行,因此删除了该节。问题是如何避免这种例外

我的数据源一切正常。我反复检查(相信我)

因此,正如线程标题所述,如何删除节的最后一行而不产生异常

谢谢


Bart

当您删除一行时,并且该行是其节的最后一行,您还需要删除该节。基本上,您需要跟踪要删除的索引路径(与行关联)和与需要删除的节相关的索引,因为它们不再包含行。您可以按如下方式进行操作:

NSMutableIndexSet *indexes = [NSMutableIndexSet indexSet];
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
[tableView deleteSections:indexes withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
每次从与tableView特定节相关的模型数组中删除对象时,请检查数组计数是否为零,在这种情况下,请将表示该节的索引添加到索引中:

[array removeObjectAtIndex:indexPath.row];
if(![array count])
    [indexes addIndex: indexPath.section];
确定与要删除的行相关的所有索引路径,然后按如下方式更新tableView:

NSMutableIndexSet *indexes = [NSMutableIndexSet indexSet];
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
[tableView deleteSections:indexes withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];

这对我和我建议的其他人都有效

也许这对你有用:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    NSUInteger section = [indexPath section];
    NSUInteger row = [indexPath row];

    NSString *key = [self.keys objectAtIndex:section];
    NSMutableArray *rowsInSection = [self.dict objectForKey:key];
    [rowsInSection removeObjectAtIndex:row];

    NSUInteger rowsInSectionCount = [rowsInSection count];

    if (rowsInSectionCount > 0) {
        // If we still have rows in this section just delete the row
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
    else {
        // There are no more rows in this section
        [self.dict removeObjectForKey:key];
        [self.keys removeObjectAtIndex:section];

        NSUInteger sectionCount = [self.keys count];

        if (sectionCount > 0) {
            // If we still have 1 or more sections left just delete this section
            [tableView deleteSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationFade];
        }
        else {
            // If there are no more rows and sections left just delete the last row
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            [tableView reloadData];
        }
    }
}

希望这就是你想要的。

这一个应该可以做到:

    // Either delete some rows within a section (leaving at least one) or the entire section.
    if ([indexPath section] < 0) {
        [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if ([indexPath section] == 0) {
        [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:[indexPath section]] withRowAnimation:UITableViewRowAnimationFade];
    }
//删除节中的某些行(至少保留一行)或整个节。
如果([indexPath节]<0){
[self.tableView deleteRowsatindExpath:[NSArray arrayWithObject:indexPath]带RowAnimation:UITableViewRowAnimationFade];
}else if([indexPath节]==0){
[self.tableView deleteSections:[NSIndexSet IndexSetWithiIndex:[IndexXPath section]]withRowAnimation:UITableViewRowAnimationFade];
}

对于Swift 2,删除模型中的数据后:

    tableView.beginUpdates()                    

    let indexSet = NSMutableIndexSet()
    indexSet.addIndex(indexPath.section)
    tableView.deleteSections(indexSet, withRowAnimation: UITableViewRowAnimation.Fade)

    tableView.endUpdates()

要删除节的最后一行,则需要删除该节,因为不再有行

在使用Tableview实现NSFetchedResultsController并利用NSFetchedResultsController处理Tableview的节、行和添加/删除/移动功能时,这一点尤为重要

    func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
    switch type {
        case .delete:
            if tableView.numberOfRows(inSection: indexPath!.section) > 1 {
                    tableView.deleteRows(at: [indexPath!], with: .automatic)
            } else {
                let section = indexPath!.section
                let indexSet = IndexSet(integer: section)
                //tableView.deleteRows(at: [indexPath!], with: .automatic)
                tableView.deleteSections(indexSet, with: .automatic)
            }
        default:
        break
    }
}
func控制器(controller:NSFetchedResultsController,didChange anObject:Any,在indexPath:indexPath?处,对于类型:NSFetchedResultsChangeType,newIndexPath:indexPath?){
开关类型{
案例.删除:
如果tableView.numberOfRows(目录:indexPath!.section)>1{
tableView.deleteRows(位于:[indexPath!],带:。自动)
}否则{
设section=indexPath.section
设indexSet=indexSet(整数:节)
//tableView.deleteRows(位于:[indexPath!],带:。自动)
tableView.deleteSections(索引集,带:。自动)
}
违约:
打破
}
}

虽然删除最后一节时会引发另一个异常,但此选项仍然有效(但我会找到解决此问题的方法)。我很难相信这是唯一的办法。这似乎太不合情合理了。它不仅仅起作用;-)。不过,感谢您的输入。要添加到这一点,当我删除节中的最后一行时,我只调用deleteSections:withRowAnimation:。我不调用deleteRowsAtIndexPaths:withRowAnimation:。动画看起来是一样的,没有抛出任何异常。你如何处理删除最后一节最后一行的问题?嗨,巴特,你是如何解决删除最后一节最后一行的问题的?“线程标题”这是一个问答网站,不是论坛。你是指问题的标题。