Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.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 如何使用选中的中心单元格创建UICollectionView?_Ios_Swift_Xcode_Storyboard - Fatal编程技术网

Ios 如何使用选中的中心单元格创建UICollectionView?

Ios 如何使用选中的中心单元格创建UICollectionView?,ios,swift,xcode,storyboard,Ios,Swift,Xcode,Storyboard,我需要开发这样的对象。 UICollectionView水平视图必须位于单元格的中心。并在同一中心标记单元格。 图片上也有: 假设您已将最后选定的单元格保存为名为savedSelection的数字变量: override func viewWillAppear(_ animated: Bool) { let idxPath = IndexPath(row: savedSelection, section: 0) let cell = collectionView?.cellF

我需要开发这样的对象。 UICollectionView水平视图必须位于单元格的中心。并在同一中心标记单元格。 图片上也有:


假设您已将最后选定的单元格保存为名为
savedSelection
的数字变量:

override func viewWillAppear(_ animated: Bool) {

    let idxPath = IndexPath(row: savedSelection, section: 0)

    let cell = collectionView?.cellForItem(at: idxPath)
    cell?.isSelected = true
    collectionView?.scrollToItem(at: idxPath, at: UICollectionViewScrollPosition.centeredHorizontally, animated: false)

}

在didSelectItemAtIndexPath上,更改偏移量?是,则单元格集中。但是,如果不是打开的视图,不是选中的中心单元,而不是选择的单元格。或者我必须单击它,然后选择它?我假设首先,没有选择任何单元格。。。接下来,用户向左/向右滚动,并选择一个单元格-将其保存在变量中。。。然后你转到另一个视图。。。当您返回时,您可以使用保存的变量将单元格自动滚动到中心,并自动选择。这可以正常工作,并应标记为正确答案。更新为Swift 5。