Invalid update: invalid number of items on UICollectionView

前端 未结 7 1014
攒了一身酷
攒了一身酷 2020-12-07 21:07

I am stumped on this. Here is my scenario. In my Appdelegate, I am creating

  1. An instance of a view controller that will be presented modally to collect two pi
7条回答
  •  旧巷少年郎
    2020-12-07 21:11

    I'm not practice with Obj-c so I write my answer in Swift.

    supposing birthdays is an array of dates as String

    birthdays = [String]()
    
    func addDate() {
       birthdays.append("your_new_date")
       collectionView.insertItems(at: [IndexPath(item: birthdays.count - 1, section: 0)] ) 
    }
    
    func removeDate() {
       if birthdays.count > 0 {
          birthdays.removeLast() // use remove(at: Int) instead if u want
          collectionView.deleteItems(at: [IndexPath(item: birthdays.count, section: 0)] )
       }
    }
    

    You should avoid this error:

    When the collectionview receives the notification I get the error:

    'Invalid update: invalid number of items in section 0. The number of items contained in an existing section after the update (1) must be equal to the number of items contained in that section before the update (1), plus or minus the number of items inserted or deleted from that section (1 inserted, 0 deleted) and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out).'

提交回复
热议问题