uicollectionview select an item immediately after reloaddata?

前端 未结 9 1163
自闭症患者
自闭症患者 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:42

    The Swift way:

    let selected = collectionView.indexPathsForSelectedItems()
    collectionView.performBatchUpdates({ [weak self] in
        self?.collectionView.reloadSections(NSIndexSet(index: 0))
        }) { completed -> Void in
            selected?.forEach { [weak self] indexPath in
                self?.collectionView.selectItemAtIndexPath(indexPath, animated: false, scrollPosition: [])
            }
    }
    

提交回复
热议问题