ios – 在UICollectionView中以编程方式选择项目

我有一个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中有这个,允许用户选择多个项目:

[self.collectionView setAllowsMultipleSelection:YES];

我想要的是,第一次CollectionView加载,一些项目通过编程方式选择,基于它们在CellTasteCollectionView中的objectId.

这是我这样做的:

- (void)collectionView:(UICollectionView *)collectionView 
         didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    Taste *taste = [self.pageTastes objectAtIndex:indexPath.item];
    printf("%sn",[taste.objectId UTF8String]);
}

当用户点击该项目时调用 – 这不是我想要的:我希望在加载UICollectionView时自动选择该项目.

我该如何做?

解决方法

我想你从 UICollectionView Class Reference错过了这个方法:
- (void)selectItemAtIndexPath:(NSIndexPath *)indexPath 
                     animated:(BOOL)animated 
               scrollPosition:(UICollectionViewScrollPosition)scrollPosition

如果需要多次选择,可以多次使用此方法.

以上是来客网为你收集整理的ios – 在UICollectionView中以编程方式选择项目全部内容,希望文章能够帮你解决ios – 在UICollectionView中以编程方式选择项目所遇到的程序开发问题。

如果觉得来客网网站内容还不错,欢迎将来客网网站推荐给程序员好友。