1

I followed UICollectionView reloadData resigns first responder in section header to implement search bar on collectionview, but when collectionView?.reloadData() is called, the screen below is shown and i've to scroll down to find the filtered cells. My code for datasource

override func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 2
}

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return section == 0 ? 0 : count // this got called after reloadData
}

override func collectionView(_ collectionView: UICollectionView,
                             cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(...)
    // this function did not get called after reloadData
}

James
  • 317
  • 2
  • 15

3 Answers3

1

Found the problem...I set my search bar height to collectionView.contentSize, which changed from 50 to 2600 after reloadData for some reason

James
  • 317
  • 2
  • 15
0

Make sure when your collectionview reload it has something to show. Count has some value if you datasource has 0 value then also your cellForItem will not be called.

vivek bhoraniya
  • 1,526
  • 2
  • 17
  • 36
0

How about you reload only the cells and not all data? After filtering the datasource array, you can compare count to collectionView.numberOfItemsInSection(1) and based on that, you can reload the cells that you need, and delete the rest or add if you need more. A perk of doing it this way is that you can also get nice filtering animations.

Ahmed Hamed
  • 504
  • 1
  • 3
  • 11