uicollectionview select an item immediately after reloaddata?

前端 未结 9 1328
自闭症患者
自闭症患者 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条回答
  •  萌比男神i
    2021-02-13 16:28

    I handled it on the willDisplayCell colelctionView delegate. The idea: A temp variable is needed to specify the initial scrolling has performed already or not (scrollIsRequired). When the last visible cell will display, than we can scroll to the required cell and set this variable to avoid scrolling again.

    - (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{
    
        //Perform any configuration
    
        if (CGRectGetMaxX(collectionView.frame) <= CGRectGetMaxX(cell.frame)) {
            // Last visible cell
            if (self.scrollIsRequired) {
                [self.collectionView selectItemAtIndexPath:[NSIndexPath indexPathForItem:self.initiallySelectedRepresentativeVerse inSection:0] animated:YES scrollPosition:UICollectionViewScrollPositionLeft];
                self.scrollIsRequired = NO;
            }
        }
    }
    

    It has worked for me like a charm.

提交回复
热议问题