Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/110.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios swift-在collectionview单元格类和collectionview控制器之间调用函数_Ios_Swift_Uicollectionview_Cell_Collectionview - Fatal编程技术网

Ios swift-在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

嗨,我一直在努力解决这个问题有一段时间了,我想不出来

我想做的是,按下collectionview单元格内的按钮,然后执行到下一个单元格的转换

这是我要调用的collectionview控制器类中的函数:

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
行,以防止由于某种原因出现空数据时应用程序崩溃