我正在将一个数组加载到 UIcollectionview
(稍后将添加其他数据)。我想随机选择 Collection View 项目:
var indexpath = Int(arc4random_uniform(UInt32(items.count)-1)
self.collectionview.selectitem(at: **indexpath**, animated:true ,scrollPosition:UICollectionViewScrollPosition)
这里它在粗体位置给我错误说:
cannot convert value of type Int to expected argument type 'IndexPath?
我可以理解这个异常,因为 indexpath
在 swift 中与集合数据索引(数组索引)不同。但是我找不到任何可以将项目索引转换为索引路径或任何其他方式的方法。
我是 iOS 的新手,所以可能不擅长为此类问题搜索正确的关键字。如果有其他方法可以满足这个要求,将得到大家的极大帮助。
最佳答案
在 swift 3 中为 UICollectionView
创建一个 IndexPath
let indexPath = IndexPath(item: 0, section: 0)
关于ios - 如何从行索引 Swift 获取索引路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41695210/