问题描述
我有 UICollectionViewController
:
- (NSInteger)collectionView:(UICollectionView *)collectionView
numberOfItemsInSection:(NSInteger)section {
return [self.pageTastes count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath {
CellTasteCollectionView *cell =
[collectionView dequeueReusableCellWithReuseIdentifier:@"Cell"
forIndexPath:indexPath];
Taste *taste = [self.pageTastes objectAtIndex:indexPath.item];
[[cell imageView] setImage:taste.image];
[cell setObjectId:taste.objectId];
return cell;
}
它有效。我在 viewDidLoad
中有这个,允许用户选择多个项目:
It works. I have this in viewDidLoad
, allowing the user to choose multiple items:
[self.collectionView setAllowsMultipleSelection:YES];
我想要的是,第一次加载CollectionView,一些项目以编程方式选择,基于他们的 objectId
在 CellTasteCollectionView
。
What I want to have, is that the first time the CollectionView loads, some items get selected programmatically, based on their objectId
in CellTasteCollectionView
.
这是怎么回事我这样做:
Here's how I'm doing this:
- (void)collectionView:(UICollectionView *)collectionView
didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
Taste *taste = [self.pageTastes objectAtIndex:indexPath.item];
printf("%s\n", [taste.objectId UTF8String]);
}
当用户点击项目时调用它 - 这不是我的意思想:我希望在 UICollectionView
加载时自动选择项目。
It's called when the user clicks on the item -- this is not what I want: I want the item to be selected automatically when UICollectionView
loads.
我该怎么做?
推荐答案
我认为你从:
- (void)selectItemAtIndexPath:(NSIndexPath *)indexPath
animated:(BOOL)animated
scrollPosition:(UICollectionViewScrollPosition)scrollPosition
如果您想要多次选择,可以多次使用此方法。
You can use this method multiple times if you want multiple selections.
这篇关于在UICollectionView中以编程方式选择项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!