swift - Invalid update: invalid number of items in section 0. -
recently got following error:
fatal exception: nsinternalinconsistencyexception invalid update: invalid number of items in section 0. number of items contained in existing section after update (13) must equal number of items contained in section before update (12), plus or minus number of items inserted or deleted section (0 inserted, 0 deleted) , plus or minus number of items moved or out of section (0 moved in, 0 moved out).
the error occurs in following code in tvos client:
let removedindexpaths = removedindexes.map({ indexpath(row: $0, section: 0) }) let addedindexpaths = addedindexes.map({ indexpath(row: $0, section: 0) }) let updatedindexpaths = updatedindexes.map({ indexpath(row: $0, section: 0) }) self.collectionview?.performbatchupdates({ self.collectionview?.deleteitems(at: removedindexpaths) self.collectionview?.insertitems(at: addedindexpaths) }, completion: { _ in guard let collectionview = self.collectionview else { return } indexpath in updatedindexpaths { if let mycell = collectionview.cellforitem(at: indexpath) as? mycollectionviewcell { let item = self.datamanager.items[indexpath.row] mycell.updateui(item) } } let collectionviewlayout = self.collectionviewlayoutfornumberofitems(self.datamanager.items.count) if collectionviewlayout.itemsize != self.collectionflowlayout.itemsize { collectionview.setcollectionviewlayout(collectionviewlayout, animated: false) } })
i using 1 section in collection view:
override func numberofsections(in collectionview: uicollectionview) -> int { return 1 }
i have checked out couple of posts on same topic, have not solved problem, guess problem in following 2 lines, not sure:
self.collectionview?.deleteitems(at: removedindexpaths) self.collectionview?.insertitems(at: addedindexpaths)
please help.
the call insertitems(at:) , deleteitems(at:) must accompanied change in datasource well.
so, before calling these apis, want change datasource, i.e. add objects before calling insertitems
, remove objects before calling deleteitems
Comments
Post a Comment