uicollectionview select an item immediately after reloaddata?

前端 未结 9 1318
自闭症患者
自闭症患者 2021-02-13 16:23

After calling -[UICollectionView reloadData] it takes some time for cells to be displayed, so selecting an item immediately after calling reloadData do

9条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-13 16:45

    This is what worked for me:

    I kept a reference of the selected index path and overide the reloadData function:

    override func reloadData() {
        super.reloadData()
        self.selectItem(at: self.selectedIndexPath, animated: false, scrollPosition: UICollectionViewScrollPosition())
    }
    

    I tried doing it using indexPathForSelectedItems, but it was creating an infinite loop on collection view load.

提交回复
热议问题