【问题标题】:Select items programmatically in UICollectionView在 UICollectionView 中以编程方式选择项目
【发布时间】:2012-11-01 12:35:23
【问题描述】:

我有一个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("%s\n", [taste.objectId UTF8String]);
}

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

我该怎么做?

【问题讨论】:

    标签: ios objective-c uicollectionview


    【解决方案1】:

    我认为您在UICollectionView Class Reference 中缺少此方法:

    - (void)selectItemAtIndexPath:(NSIndexPath *)indexPath 
                         animated:(BOOL)animated 
                   scrollPosition:(UICollectionViewScrollPosition)scrollPosition
    

    如果要多选,可以多次使用此方法。

    【讨论】:

    • 请注意,以编程方式调用此方法不会触发 collectionView:didSelectItemAtIndexPath:.
    【解决方案2】:

    如果您以编程方式调用selectItem,则不会调用didSelectItemAt。您应该在它之后手动调用该方法。

    self.collectionView.selectItem(at: IndexPath(item: 0, section: 0), animated: true, scrollPosition: .bottom)
    self.collectionView(self.collectionView, didSelectItemAt: IndexPath(item: 0, section: 0))
    

    【讨论】:

      【解决方案3】:

      为了正确的行为,连续调用 4 个函数:

      // Deselect
      self.collection.deselectItem(at: previousPath, animated: true)
      self.collectionView(self.collection, didDeselectItemAt: previousPath)
      
      // Select
      self.collection.selectItem(at: path, animated: true, scrollPosition: .centeredVertically)
      self.collectionView(self.collection, didSelectItemAt: path)
      

      【讨论】:

        【解决方案4】:
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
        [self.districtTableview selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionTop];
        [self tableView:weakSelf.districtTableview didSelectRowAtIndexPath:indexPath];
        

        我在tableview中使用了上面的方法,效果很好。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-03-02
          • 2017-12-11
          • 2010-10-08
          • 2010-11-07
          • 1970-01-01
          • 2012-07-17
          • 1970-01-01
          相关资源
          最近更新 更多