无法以编程方式在UICollectionView中选择项目

时间:2017-07-31 10:04:43

标签: ios swift uicollectionview uicollectionviewcell

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

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
}

我忘记了什么吗?

7 个答案:

答案 0 :(得分:12)

来自selectItem(at:animated:scrollPosition:)的{​​{3}}

  

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

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

答案 1 :(得分:7)

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

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

答案 2 :(得分:3)

let indexPath = IndexPath(item: 0, section: 0)
DispatchQueue.main.async {
   self.collectionView.selectItem(at: indexPath, animated: true, scrollPosition: .left)
}

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

答案 3 :(得分:2)

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

答案 4 :(得分:2)

对于我来说,我需要结合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))
}

答案 5 :(得分:0)

只是纠正我上面的答案

self.collectionView.selectItem(at: IndexPath(item: 0, section: 0), animated: true, scrollPosition: .left)
self.collectionView.delegate?.collectionView!(collectionView, didSelectItemAt: IndexPath(item: 0, section: 0))

答案 6 :(得分:0)

以上答案均无济于事,因为IndexPath(item: 0, section: 0)collectionView中的单元格不可见,因此我的应用程序崩溃了。比起我,我滚动到[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])
}
相关问题