ios – 在首次加载UICollectionView后如何选择某些项目?

前端之家收集整理的这篇文章主要介绍了ios – 在首次加载UICollectionView后如何选择某些项目?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试着写 @H_301_2@[self collectionView:myCollectionView didSelectItemAtIndexPath:selectedIndexPath]

@H_301_2@并且在ViewDidLoad中选择了UICollectionViewCell = YES,并且它确实实现了方法didSelectItemAtIndexPath,但是未选择该单元格.

@H_301_2@我在UICollectionViewCell子类的(void)setSelected:(BOOL)中选择了所选状态.视图加载后,手动选择功能有效.但是我不能让它在视图的第一次加载之后自动选择一些项目.

@H_301_2@我试图写代码

@H_301_2@ – (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

@H_301_2@和

@H_301_2@ – (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath,一切都不行.

@H_301_2@我发现它首先运行viewDidLoad和didSelectItemAtIndexPath,然后是cellForItemAtIndexPath,似乎我不能在cellPath(我知道)之前获取单元格在cellForItemAtIndexPath之前,因为之前的单元格不存在.那么在首次加载UICollectionView后如何选择一些项目?

@H_301_2@对不起我可怜的英语提前致谢.

解决方法

不确定,如果您的问题正确,但这是一个可能的解决方案: @H_301_2@在例如viewWillAppear:做

[self.collectionView reloadData];
NSIndexPath *selection = [NSIndexPath indexPathForItem:THE_ITEM_TO_SELECT 
                                             inSection:THE_SECTION];
[self.collectionView selectItemAtIndexPath:selection 
                                  animated:YES 
                            scrollPosition:UICollectionViewScrollPositionNone];
@H_301_2@记住,以编程方式调用“selectItemAtIndexPath”不调用相关的委托方法;如果需要,您必须在代码调用它们.

猜你在找的iOS相关文章