【问题标题】:How to select item in collectionview programmatically?如何以编程方式选择collectionview中的项目?
【发布时间】:2014-02-07 23:05:06
【问题描述】:

我有一个UICollectionViewUICollectionViewdatasource 是学生的NSArray(来自CoreData)。我希望当前学生项目为selected/highlighted

我该怎么做?我知道有一个方法:

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

NSIndexPathUICollectionViewScrollPosition 作为参数。

我有数据源(从CoreData 填充的学生的NSArray)和学生对象是selected

那么,我如何获得NSIndexPathUICollectionViewScrollPosition? 或者有没有其他方法可以突出显示一个项目?

【问题讨论】:

标签: ios iphone uicollectionview uicollectionviewcell


【解决方案1】:

你可以在[self.collectionView reloadData]之后直接使用这个

[self.collectionView 
   selectItemAtIndexPath:[NSIndexPath indexPathForItem:index inSection:0] 
                animated:YES
          scrollPosition:UICollectionViewScrollPositionCenteredVertically];

其中index 是所选学生的索引号。

【讨论】:

  • 对我不起作用,因为也调用了 didSelectItemAtIndexPath 并且不知何故这会干扰。我的解决方案是在 cellForItemAtIndexPath 中突出显示当前学生单元格,当用户切换到其他学生时,取消突出显示在 didDeselectItemAtIndexPath 中未选中的所有单元格。
  • hmm,可以查看didSelectItemAtIndexPath中的item,看看选中的学生索引是否等于选中行的索引。如果不是,请取消选择其他行。我说的对吗?
  • 晚会,但它可以帮助人们。在 Apple 的 optional func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) 文档中,当用户在集合视图中成功选择项目时,集合视图会调用此方法。当您以编程方式设置选择时,它不会调用此方法。
  • 大家好,请检查我的答案请stackoverflow.com/a/60863297/2715840
【解决方案2】:

斯威夫特

let indexPath = self.collectionView.indexPathsForSelectedItems?.last ?? IndexPath(item: 0, section: 0)
self.collectionView.selectItem(at: indexPath, animated: false, scrollPosition: UICollectionView.ScrollPosition.centeredHorizontally)

【讨论】:

    【解决方案3】:

    根据您的问题,我假设您只有一个选定的学生,我用一组用户可以选择的图标做了类似的事情。 首先,我确实加载了:

    override func viewDidLoad() {
        super.viewDidLoad()
    
        iconCollectionView.delegate = self
        iconCollectionView.dataSource = self
        iconCollectionView.allowsMultipleSelection = false
        iconCollectionView.selectItemAtIndexPath(NSIndexPath(forItem: 0, inSection: 0), animated: false, scrollPosition: .None)
    }
    

    这里我默认选择第一个单元格,您可以使用StudentArray.indexOf 来获取您选择的学生索引。然后显示我选择了哪个项目:

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = iconCollectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! IconCollectionViewCell
        cell.imageView.image = UIImage(named: imageResourceNames.pngImageNames[indexPath.row])
        if cell.selected {
            cell.backgroundColor = UIColor.grayColor()
        }
        return cell
    }
    

    当集合首次显示时调用,然后对选择的变化做出反应:

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        collectionView.cellForItemAtIndexPath(indexPath)?.backgroundColor = UIColor.grayColor()
    }
    
    func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
        collectionView.cellForItemAtIndexPath(indexPath)?.backgroundColor = UIColor.clearColor()
    }
    

    显然有很多方法可以显示单元格选择,我的方法很简单,但这就是我需要做的。

    编辑:自从发布上述内容后,我意识到在单元类中向selected 添加观察者更简单:

    class IconCollectionViewCell: UICollectionViewCell {
    
    ...
    
        override var selected: Bool {
            didSet {
                backgroundColor = selected ? UIColor.grayColor() : UIColor.clearColor()
            }
        }
    }
    

    有了这个,就不需要处理didSelectdidDeselect 或检查cellForItemAtIndexPath 中的选择,单元格会自动执行。

    【讨论】:

    • Apples 文档说明如下:“此方法不会导致调用任何与选择相关的委托方法。”调用 select 函数时,不会调用委托方法,因此不会有视觉提示表明您的第一个单元格已被选中。
    【解决方案4】:

    我知道超级晚了,但只是为了记录,对于 y 部分中的项目 x:

     NSIndexPath *indexPath = [NSIndexPath indexPathForItem:x inSection:y];
    

    如果您只想以编程方式突出显示某个项目,则需要在集合视图本身上调用 selectItemAtIndexPath 方法,例如:

    [theCollectionView selectItemAtIndexPath:indexPath
        animated:YES scrollPosition:UICollectionViewScrollPositionCenteredVertically];
    

    如果你想以编程方式模拟选择一个项目,并且你在 do select 被调用时获得了代码,你需要在视图控制器本身上调用 selectItemAtIndexPath 方法并将你的集合视图作为参数传递给它:

    [theViewController collectionView:theCollectionView didSelectItemAtIndexPath:indexPath];
    

    现在可以确定的是,为了选择一个项目并调用选择代码,您需要同时调用这两个:D

    [theCollectionView selectItemAtIndexPath:indexPath
        animated:YES scrollPosition:UICollectionViewScrollPositionCenteredVertically];
    
    [theViewController collectionView:theCollectionView didSelectItemAtIndexPath:indexPath];
    

    SWIFT 版本:

    let indexPath = IndexPath(item: x, section: y)
    theCollectionView.selectItem(at: indexPath, animated: true, scrollPosition: .centeredVertically)
    theViewController.collectionView(theCollectionView, didSelectItemAt: indexPath)
    

    享受吧!

    【讨论】:

    • 我使用它在创建collectionView时预初始化它的状态。要使其工作,您需要调用 代码在此处} 中的 DispatchQueue.main.async {() 中描述的代码
    【解决方案5】:

    签名有变化。

    didSelectItemAtIndexPath  to didSelectItemAt 
    

    请检查一下:

    _ collectionView
    

    请尝试一下:

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath){
    
            print(" selected",)
        }
    

    【讨论】:

      【解决方案6】:
      let indexPathForFirstRow = IndexPath(row: 0, section: 0)
          paymentHeaderCollectionView.selectItem(at: indexPathForFirstRow, animated: false, scrollPosition: UICollectionViewScrollPosition.left)
          self.collectionView(paymentHeaderCollectionView, didSelectItemAt: indexPathForFirstRow)
      

      【讨论】:

        【解决方案7】:

        使用 Swift:

        您可以简单地执行此操作来获取collectiveView 的索引并使用该键从您的数组中获取数据

        collectionView.indexPathsForSelectedItems?.last?.row
        

        这会给你所选项目的索引为整数

        【讨论】:

          【解决方案8】:

          SWIFT 5

          collView.selectItem(at: NSIndexPath(item: 0, section: 0) as IndexPath, animated: false, scrollPosition: .centeredHorizontally)
          

          【讨论】:

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