簡體   English   中英

UITableView beginUpdates() 和 endUpdates() 說明

[英]UITableView beginUpdates() and endUpdates() clarification

我希望有人可以澄清UITableViewbeginUpdates()endUpdates()的用法。

如果我有多個更改( deleteRowsinsertRowsmoveRow等)調用,我是否應該像以前的調用已經完成一樣使用后續調用?

例如:

我的數據是 [A, B, C](假設只有 1 個部分),我已經將數據源更新為 [B, D, C] 並且我希望刪除 A,然后在 B 和 C 之間插入 D,我應該這樣做下列:

tableView.beginUpdates()
tableView.deleteRows(at: [IndexPath(row: 0, section: 0), with: .automatic)
tableView.insertRows(at: [IndexPath(row: 1, section: 0), with: .automatic)
tableView.endUpdates()

我假設刪除會在插入之前發生或

tableView.beginUpdates()
tableView.deleteRows(at: [IndexPath(row: 0, section: 0), with: .automatic)
tableView.insertRows(at: [IndexPath(row: 2, section: 0), with: .automatic)
tableView.endUpdates()

刪除還沒有發生的地方?

我正在使用 Swift 4,但目標是 iOS 9,所以我不能專門使用performBatchUpdates 非常感謝。

我想我已經找到了我正在尋找的澄清。 https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/TableView_iPhone/ManageInsertDeleteRow/ManageInsertDeleteRow.html ,在標題“行和節的批量插入、刪除和重新加載”和副標題“Ordering of Rows and Sections”下操作和索引路徑"

您可能已經注意到清單 7-8 中顯示的代碼中有些東西看起來很奇怪。 代碼在調用 insertRowsAtIndexPaths:withRowAnimation: 之后調用 deleteRowsAtIndexPaths:withRowAnimation: 方法。 但是,這不是 UITableView 完成操作的順序。 它將任何行或節的插入推遲到它處理了行或節的刪除之后。 表格視圖的行為與在更新塊內調用的重新加載方法相同——重新加載發生在執行動畫塊之前的行和節的索引。 無論插入、刪除和重新加載方法調用的順序如何,都會發生此行為。

動畫塊中的刪除和重新加載操作指定應刪除或重​​新加載原始表中的哪些行和部分; 插入指定應將哪些行和節添加到結果表中。 用於標識節和行的索引路徑遵循此模型。 另一方面,在可變數組中插入或刪除項目可能會影響用於連續插入或刪除操作的數組索引; 例如,如果您在某個索引處插入一個項目,則數組中所有后續項目的索引都會遞增。

雖然這幾乎無法解析,但我認為這意味着問題中的第二個示例是正確的。

暫無
暫無

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

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