Ios swift-在collectionview单元格类和collectionview控制器之间调用函数
嗨,我一直在努力解决这个问题有一段时间了,我想不出来 我想做的是,按下collectionview单元格内的按钮,然后执行到下一个单元格的转换 这是我要调用的collectionview控制器类中的函数:Ios swift-在collectionview单元格类和collectionview控制器之间调用函数,ios,swift,uicollectionview,cell,collectionview,Ios,Swift,Uicollectionview,Cell,Collectionview,嗨,我一直在努力解决这个问题有一段时间了,我想不出来 我想做的是,按下collectionview单元格内的按钮,然后执行到下一个单元格的转换 这是我要调用的collectionview控制器类中的函数: func done(){ let scrollIndex: NSIndexPath = NSIndexPath(item: 1, section: 0)// NSIndexPath(forItem: 0, inSection: 0) CollectionViewet.scrol
func done(){
let scrollIndex: NSIndexPath = NSIndexPath(item: 1, section: 0)// NSIndexPath(forItem: 0, inSection: 0)
CollectionViewet.scrollToItem(at: scrollIndex as IndexPath, at: .centeredHorizontally, animated: true)
}
当我从collectionview控制器调用一个函数时,它会告诉我
在展开可选值时意外发现nil
我做错了什么?默认情况下,
UICollectionViewController
有一个子视图,名为collectionView
,您应该使用该子视图滚动到项目
因此,如果您正确覆盖数据源委托方法,您应该能够如下滚动
func done() {
let numberOfRows = collectionView?.numberOfItems(inSection: 0) ?? 0
guard numberOfRows > 1 else { return }
let indexPath = IndexPath(row: 1, section: 0)
collectionView?.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)
}
我添加了guard
行,以防止由于某种原因出现空数据时应用程序崩溃