我需要以不同的方式显示集合视图。我有一个items数组,如下所示:
items = ["A","B","C","D","E","F","G","H","I"]
然后,我需要显示收藏集视图,如下所示:
如何实现这种实现方式?
我尝试实现以下方式:
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
}
但是现在结果如下所示:
答案 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