[英]collection view list cell when the cell is added / deleted, how to update the indexPath of the cell?
我正在處理集合視圖列表單元格,我想知道當我在 iOS14 上使用 diffable 數據源和快照時如何更新 indexPath。
在我的 ViewController 的 viewdidload 中,我調用了注冊單元信息的 configureDataSource 方法。 另外,我有一個 displayEvents 方法,它從后端獲取事件數據並將它們顯示在集合視圖單元格上。 eventDataSource 包含一個事件數組,因此當獲取事件數據時,它會更新數組,以便 ViewController 可以顯示數據。
我還有一個 addEvent function,它是從 EventActionModalViewController 調用的,就像用戶鍵入事件名稱並發送.save API 請求以將數據存儲在數據庫中一樣。 它工作正常,我的意思是成功添加數據后端並在集合視圖列表上顯示新事件。 但是,問題是集合視圖的 indexPath 沒有更新。
例如,如果集合視圖中已經有兩個事件,它們的 indexPath 是[0,0] and [0,1]
。 (我在print("here is the indexPath: \(indexPath)")
它們打印出來)並且添加新事件后,集合視圖上有三個事件,這是正確的,但是 indexPath 變為[0,0],[0,0] and [0,1]
。 所以我認為已經顯示事件的 indexPath 沒有更新。 應用快照不足以更新每個單元格的 indexPath 嗎? 我在想即使在應用快照之后,它仍然需要重新加載集合視圖以將新的 indexPath 應用到單元格,或者類似的東西。
有沒有人遇到過同樣的問題? 如果是這樣,您是如何將新的 indexPath 應用到單元格的? 我還有一個 function 來刪除單元格,但它也不會更新 indexPath。 順便說一句,我正在開發適用於 iOS14 的應用程序,所以不能使用適用於 iOS15 的應用程序...
private var eventDataSource = EventDataSource()
override func viewDidLoad() {
super.viewDidLoad()
setupCollectionView() // -> create collection view and set constraints
configureDataSource()
displayEvents()
}
func configureDataSource() {
let cellRegistration = UICollectionView.CellRegistration<ListCell, Event> { cell, indexPath, Event in
cell.Event = Event
let moreAction = UIAction(image: Images.setting) { _ in
let vc = EventActionModalViewController();
vc.modalPresentationStyle = .overCurrentContext
print("here is the indexPath: \(indexPath)")
vc.indexPath = indexPath
self.tabBarController?.present(vc, animated: false, completion: nil)
}
let moreActionButton = UIButton(primaryAction: moreAction)
moreActionButton.tintColor = UIColor.ouchienLightGray()
let moreActionAccessory = UICellAccessory.CustomViewConfiguration(
customView: moreActionButton,
placement: .trailing(displayed: .whenEditing, at: { _ in return 0 })
)
cell.accessories = [
.customView(configuration: moreActionAccessory),
.disclosureIndicator(displayed: .whenNotEditing, options: .init(tintColor: .systemGray))
]
}
dataSource = UICollectionViewDiffableDataSource<Section, Event>(collectionView: collectionView) {
(collectionView, indexPath, Event) -> UICollectionViewCell? in
let cell = collectionView.dequeueConfiguredReusableCell(using: cellRegistration, for: indexPath, item: Event)
return cell
}
}
func displayEvents() {
eventDataSource.fetchEvents { [weak self] in
self?.applySnapshot(animatingDifferences: true)
}
}
func addEvent(EventTitle: String) {
eventDataSource.save(eventTitle: EventTitle, completion: { [weak self] in
self?.applySnapshot(animatingDifferences: true)
})
}
func applySnapshot(animatingDifferences: Bool = true) {
snapshot = NSDiffableDataSourceSnapshot<Section, Event>()
snapshot.appendSections([.List])
snapshot.appendItems(eventDataSource.Events)
dataSource?.apply(snapshot, animatingDifferences: animatingDifferences)
}
您在moreAction
中傳遞的索引路徑是在單元格注冊時捕獲的路徑,只有在創建單元格時才會發生這種情況,但不會被回收。
您需要隨時調用UICollectionView.indexPathForCell
來獲取正確的索引。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.