簡體   English   中英

如何刪除部分的最后一行?

[英]How to delete the last row of a section?

這個問題在過去的幾個小時里一直讓我很忙。 我有兩個部分,每部分一行。 當我刪除其中一個部分中的行時,它會引發異常,指出這是無效更新(更新前后的行數/部分數不相同)。 這是可以理解的,因為我刪除了一個部分的最后一行,因此我刪除了該部分。 問題是如何避免異常。

我的數據源一切正常。 我檢查並重新檢查(相信我)。

因此,正如線程的標題所述,如何在不出現異常的情況下刪除部分的最后一行?

謝謝,

巴特

當您刪除一行,並且該行是其部分的最后一行時,您還需要刪除該部分。 基本上,您需要跟蹤要刪除的與行相關聯的 indexPath,以及與需要刪除的部分相關的索引,因為它們不再包含行。 你可以這樣做:

NSMutableIndexSet *indexes = [NSMutableIndexSet indexSet];

每次從模型數組中刪除與 tableView 的特定部分相關的對象時,請檢查數組計數是否為零,在這種情況下,將表示該部分的索引添加到索引:

[array removeObjectAtIndex:indexPath.row];
if(![array count])
    [indexes addIndex: indexPath.section];

確定與要刪除的行相關的所有indexPaths,然后按如下方式更新tableView:

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

對於 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
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM