uicollectionview select an item immediately after reloaddata?

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

    create a method that does the selection and call it using performSelector after calling reload e.g;

    [self performSelector:@selector(selectIt) withObject:self afterDelay:0.1];
    
    0 讨论(0)
  • 2021-02-13 16:52

    I'm handling selection of cells in collectionView: cellForItemAtIndexPath:. The problem I found was that if the cell didn't exist, simply calling selectItemAtIndexPath: animated: scrollPosition: wouldn't actually select the item.

    Instead you have to do:

    cell.selected = YES;
    [m_collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题