【问题标题】:Can't select item in UICollectionView programmatically无法以编程方式选择 UICollectionView 中的项目
【发布时间】:2017-07-31 10:04:42
【问题描述】:

我正在尝试在以这种方式加载视图时设置默认项:

override func viewDidLoad() {
   super.viewDidLoad()

   self.collectionView.delegate = self
   self.collectionView.dataSource = self
   self.collectionView.allowsSelection = true
   self.collectionView.allowsMultipleSelection = true

}

我正在尝试在viewDidAppear 方法中选择一个项目:

override func viewDidAppear(_ animated: Bool) {
    DispatchQueue.main.async(execute: {
        self.collectionView.selectItem(at: IndexPath(item: 0, section: 0), animated: true, scrollPosition: UICollectionViewScrollPosition.bottom)
    })
}

但是 didSelectItemAt 方法并没有像我需要的那样被触发。

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath){
   //some config
}

我是不是忘记了什么?

【问题讨论】:

    标签: ios swift uicollectionview uicollectionviewcell


    【解决方案1】:

    如果您以编程方式调用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))
    

    【讨论】:

    • 调用self.collectionView.reloadItems(at: [indexPath]) 可能会更好,因为屏幕外的单元格可能会使您的应用程序崩溃。不过,这取决于您的单元格的逻辑
    【解决方案2】:

    来自selectItem(at:animated:scrollPosition:)的documentation

    此方法不会导致调用任何与选择相关的委托方法。

    这意味着您必须手动调用委托方法。

    【讨论】:

      【解决方案3】:
      let indexPath = IndexPath(item: 0, section: 0)
      DispatchQueue.main.async {
         self.collectionView.selectItem(at: indexPath, animated: true, scrollPosition: .left)
      }
      

      这是对我有用的解决方案。希望这会帮助你。

      【讨论】:

      • 有用。谢谢。
      【解决方案4】:
      override func viewDidAppear(_ animated: Bool) {
          super.viewDidAppear(animated)
      
      self.activityCollectionView?.scrollToItem(at: IndexPath(row: 1, section: 0), at: UICollectionViewScrollPosition.right, animated: true)
      
      }
      
      //viewDidAppear is the key
      

      【讨论】:

        【解决方案5】:

        对于我的情况,我需要结合 Tamàs 和 iDev750 的回答:

        DispatchQueue.main.async {
            self.collectionView.selectItem(at: IndexPath(item: 0, section: 0), animated: true, scrollPosition: .bottom)
            self.collectionView(self.collectionView, didSelectItemAt: IndexPath(item: 0, section: 0))
        }
        

        【讨论】:

        • 当我在没有 DipatchQueue.main.async 的情况下调用它时,didSelectItemAt 函数被调用但 collectionView.cellForItem(at: indexPath) 返回 nil。
        【解决方案6】:

        上述答案均不适用于我,因为我的collectionView 中的IndexPath(item: 0, section: 0) 中的单元格不可见,因此我的应用程序崩溃了。比我通过滚动到 [0,0] 并等待 collectionView 完成其动画然后手动调用委托方法 didSelectItemAt 选择项目来修复它。现在它完美地工作了。

        示例代码:

        /** scroll to cell that you want to select it */
        layoutsCollectionView.selectItem(at: .init(item: 0, section: 0),
                                                 animated: true,
                                                 scrollPosition: .centeredHorizontally)
        
        /** implement UIScrollViewDelegate in your view controller and call */
        func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
        
            /** 
             * here scroll animation finished thats mean your cell is visible
             * now you can select your cell with delegate method
             */
            layoutsCollectionView.delegate?.collectionView?(self.layoutsCollectionView, didSelectItemAt: [0,0])
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-03-02
          • 2017-12-11
          • 2012-07-17
          • 2014-09-27
          • 2011-09-01
          • 1970-01-01
          • 2011-04-30
          • 1970-01-01
          相关资源
          最近更新 更多