简体   繁体   中英

Programmatically selecting UICollectionView cell which are not loaded yet

I am trying to programmatically scroll to UICollectionView cell using

[_calendarView collectionView:_calendarView.calendarCollectionView didSelectItemAtIndexPath:indexpath];

When I retrieve cell using that index path, it gives me nil cell. I think this cell is not loaded yet. Any solution.

First, set an instance variable to indicate you want the cell selected.

self.selectedIndexPath = indexPath;

Next, scroll to the cell using:

[_calendarView scrollToItemAtIndexPath:indexPath atScrollPosition: UICollectionViewScrollPositionNone animated:YES];

When returning a cell from collectionView:cellForItemAtIndexPath: , compare to the instance variable. When you have a match, select the cell and then set the instance variable to nil .

Try Using the below method of UICollectionViewDelegate:

- (void)scrollToItemAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UICollectionViewScrollPosition)scrollPosition animated:(BOOL)animated;

params-

indexpath : index path of the cell

animated : YES/NO.

scrollPosition : Position of cell when scrolled. choose from following options:

Vertical positions-

UICollectionViewScrollPositionTop                  
UICollectionViewScrollPositionCenteredVertically   
UICollectionViewScrollPositionBottom

Horizontal Positions-

UICollectionViewScrollPositionLeft  
UICollectionViewScrollPositionCenteredHorizontally 
UICollectionViewScrollPositionRight 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM