ios 首次加载后如何在 UICollectionView 中选择一些项目?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15228472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to select some items in the UICollectionView after it first load?
提问by zgjie
I tried to write
我试着写
[self collectionView:myCollectionView didSelectItemAtIndexPath:selectedIndexPath]
[self collectionView:myCollectionView didSelectItemAtIndexPath:selectedIndexPath]
and UICollectionViewCell's selected=YES in viewDidLoad, and it did implemented the method didSelectItemAtIndexPath, but the cell not selected.
和 UICollectionViewCell 的 selected=YES 在 viewDidLoad 中,它确实实现了方法didSelectItemAtIndexPath,但没有选择单元格。
I wrote the selected state in UICollectionViewCell subclass's (void)setSelected:(BOOL)selected. After the view was load, the manual selection function works. But I could't let it auto selected some items after the view's first load.
我在 UICollectionViewCell 子类的(void)setSelected:(BOOL)selected. 加载视图后,手动选择功能起作用。但是我不能让它在视图第一次加载后自动选择一些项目。
And I tried to write codes in:
我尝试在以下位置编写代码:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
and
和
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath, all not OK.
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath,都不好。
I found it first run viewDidLoadand didSelectItemAtIndexPath, then cellForItemAtIndexPath, it seems like that I could't get the cell in the indexPath(that I know) before cellForItemAtIndexPath, because before that the cell is not exist. So how to select some items in the UICollectionViewafter it first load?
我发现它第一次运行viewDidLoad和didSelectItemAtIndexPath,然后cellForItemAtIndexPath,好像,我无法让细胞在indexPath(我知道)之前cellForItemAtIndexPath,因为在此之前,该小区是不存在的。那么如何在UICollectionView第一次加载后选择一些项目呢?
Sorry for my poor english. Thanks in advance.
对不起,我的英语不好。提前致谢。
采纳答案by SAE
Not sure, if got your question correct, but here's a possible solution:
不确定,如果您的问题正确,但这是一个可能的解决方案:
In e.g. viewWillAppear:do
在例如viewWillAppear:做
[self.collectionView reloadData];
NSIndexPath *selection = [NSIndexPath indexPathForItem:THE_ITEM_TO_SELECT
inSection:THE_SECTION];
[self.collectionView selectItemAtIndexPath:selection
animated:YES
scrollPosition:UICollectionViewScrollPositionNone];
Bear in mind, that calling 'selectItemAtIndexPath' programmatically does NOT call the related delegate methods; you'll have to call them in code if you need them.
请记住,以编程方式调用“selectItemAtIndexPath”不会调用相关的委托方法;如果需要,您必须在代码中调用它们。
回答by Andrii Chernenko
In my case selectItemAtIndexPathhad no effect after reloadData, so I had to call it in the completion block of performBatchUpdates:
在我的情况下selectItemAtIndexPath没有影响之后reloadData,所以我不得不在完成块中调用它performBatchUpdates:
collectionView.dataSource = ...
collectionView.delegate = ...
let indexPath = ...
collectionView.performBatchUpdates(nil) { _ in
collectionView.selectItemAtIndexPath(indexPath, animated: false, scrollPosition: .None)
}
回答by Onur Erkan
Swift 3
斯威夫特 3
Implement this override function where is your collectionview is created. Assuming that we have 1 section 1 row like Instagram stories.
在创建 collectionview 的位置实现此覆盖函数。假设我们有 1 个第 1 行,如 Instagram 故事。
override func viewDidAppear(_ animated: Bool) {
// Auto Select First Item
self.myCollectionView.performBatchUpdates(nil) { _ in
self.myCollectionView.selectItem(at: IndexPath(item: 0, section: 0), animated: false, scrollPosition: [.centeredHorizontally])
self.collectionView(self.myCollectionView, didSelectItemAt : IndexPath(item: 0, section: 0))
}
}

