如何从行索引Swift获取indexpath [英] How to get indexpath from row index Swift

查看:781
本文介绍了如何从行索引Swift获取indexpath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将数组加载到 UIcollectionview (稍后将添加其他数据)。我想随机选择集合视图项:

  var indexpath = Int(arc4random_uniform(UInt32(items.count)-1) 

self.collectionview.selectitem(at:** indexpath **,animation:true,scrollPosition:UICollectionViewScrollPosition)

在这里它给我粗体显示错误:


无法将Int类型的值转换为预期参数输入'IndexPath?


我可以理解此异常,因为 indexpath 在迅速从集合数据索引(数组索引)中获取数据,但是我找不到任何可以将项目索引转换为索引路径或其他方式的方法。



我对iOS较新


解决方案

在Swift 3中为 UIColl创建 IndexPath ectionView

  let indexPath = IndexPath(item:0,section:0)


I am loading an array to UIcollectionview(later will add other data) . I want to select collection view item randomly as:

var indexpath = Int(arc4random_uniform(UInt32(items.count)-1) 

self.collectionview.selectitem(at: **indexpath**, animated:true ,scrollPosition:UICollectionViewScrollPosition)

Here it’s giving me error at bold position saying:

cannot convert value of type Int to expected argument type 'IndexPath?

I can understand this exception as indexpath is different in swift from collection data index(array index). But I am unable to find any method that can convert item index to indexpath or anyother way.

I am newer to iOS so might be not good at searching correct keywords for such issue. It will be a great favour from you all for any other method to this requirement.

解决方案

To create an IndexPath in swift 3 for UICollectionView

let indexPath = IndexPath(item: 0, section: 0)

这篇关于如何从行索引Swift获取indexpath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆