uicollectionview select an item immediately after reloaddata?

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

    0 讨论(0)
  • 2021-02-13 16:30

    Don't use reloadData

    Use - (void)performBatchUpdates:(void (^)(void))updates completion:(void (^)(BOOL finished))completion instead. The completion block is executed after animations for cell insertion/deletion etc. have completed. You can put the call to reloadData in the (void (^)(void))updates block

    0 讨论(0)
  • 2021-02-13 16:34

    Make sure you're calling reloadData on the main thread. That could be the cause for the delay in your cell updates.

    0 讨论(0)
  • 2021-02-13 16:39

    Along the lines of this answer I found that calling layoutIfNeeded after reloadData seemed to effectively 'flush' the reload before I do other things to the collectionView:

    [self.collectionView reloadData];
    [self.collectionView layoutIfNeeded];
    ...
    

    On the page I found this solution, some commenters indicated it didn't work for them on iOS 9, but it's been fine for me so your mileage may vary.

    0 讨论(0)
  • 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: [])
            }
    }
    
    0 讨论(0)
  • 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.

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