如何在集合视图中显示从数组到特定indexPath的项目

时间:2019-08-23 06:15:08

标签: ios swift iphone uicollectionview

我需要以不同的方式显示集合视图。我有一个items数组,如下所示:

items = ["A","B","C","D","E","F","G","H","I"]

然后,我需要显示收藏集视图,如下所示:

enter image description here

如何实现这种实现方式?

我尝试实现以下方式:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

                let cell = chartCollectionView.dequeueReusableCell(withReuseIdentifier: "ViewCell2", for: indexPath) as! ViewCell2
                cell.lblTitle.text = self.getDataFromArray(index: indexPath.row)
                return cell
   }

但是现在结果如下所示:

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以使用方法shuffle()随机化数据,该方法是对func进行突变的 或shuffled()返回随机元素数组

items = ["A","B","C","D","E","F","G","H","I"].shuffled()

复杂度:O(n),其中n是集合的长度。

文档:

https://developer.apple.com/documentation/swift/array/2994753-shuffle https://developer.apple.com/documentation/swift/array/2994757-shuffled