uicollectionview select an item immediately after reloaddata?

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

    Apple says:

    You should not call this method in the middle of animation blocks where items are being inserted or deleted. Insertions and deletions automatically cause the table’s data to be updated appropriately.

    In fact, you should not call this method in the middle of any animation (including UICollectionView in the scrolling).

    So, you can use:

    [self.collectionView setContentOffset:CGPointZero animated:NO];
    [self.collectionView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
    

    or mark sure not any animation, and then call reloadData; or

    [self.collectionView performBatchUpdates:^{
    //insert, delete, reload, or move operations
    } completion:nil];
    

    Hope this is helpful to you.

提交回复
热议问题