簡體   English   中英

tableview 刪除部分中的最后一行

[英]tableview remove last row in section

我在 stackoverflow/google 上做了很多小時的研究,並研究了幾篇關於類似問題的帖子。 然而,他們都沒有解決我遇到的特定問題。

簡而言之,我有一個 NSMutableDictionay(-ies) 的 NSMutableArray。 每個字典都包含一個 NSMutableArray。 當我刪除數組中的最后一項時,我也想刪除字典。 用tableview的話來說,我想在刪除該部分的最后一行時刪除該部分。

我確信數據源是一致的。

這篇文章在這里: UITableView:使用 animation 刪除部分建議如果要刪除的行是最后一行,則直接刪除該部分。

如果該部分是我的根數組中的最后一個部分,這可以正常工作。

我發現的錯誤是,如果該部分不是最后一個部分,則后面的部分將取代它。 當系統正在繪制刪除 animation 時,它會拋出一個NSInternalInconsistencyException

說,我有第 0 節和第 1 節,都只有一行。 當我刪除 indexPath (0,0) 時,我檢測到第 0 節應該是 go 因為它只有一行,然后我在commitEditingStyle中刪除第 0 節

拋出異常是因為用戶刪除了 Section 0 中的 Row 0,所以生成的 Section 0 中的行數應該從 1 變為 0。但是,刪除之后,舊的 Section 1 現在變成了 Section 0,它會返回行數在第 0 節中是 1。

這里有什么建議嗎? 還是我錯過了一些非常簡單的東西?

我在 commitEditingSytle 中的代碼:

[tableView beginUpdates];
[... remove the  item from array of this dictionary...]
// if last row in section, remove the section
NSNumber *section = [NSNumber numberWithUnsignedInteger: indexPath.section];
if ([[FoodTypes subtypes:section] count] == 0) 
{
    [...remove the dictionary from root array...]

    [tableView deleteSections: [NSIndexSet indexSetWithIndex: indexPath.section] withRowAnimation:YES];
}
else 
{
    // otherwise, remove the row only
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
[tableView endUpdates];

如果你想從你的數組中刪除一個字典,它的 object 作為下標變量,那么你需要這一行。

[yourObject removeObjectAtIndex:indexPath.section];

然后通過這一行重新加載表格

[yourTable reloadData];

現在,由於您的表依賴於此數據源,因此它會自動從表中刪除。

暫無
暫無

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

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