var nameArrays = [
1: ["twitter", "twitter", "", "twitter", "twitter", "twitter", "twitter", "twitter", "twitter", "twitter", "twitter", "twitter", "twitter", "twitter", "twitter", "twitter", "twitter", "twitter", "twitter", "twitter", "twitter", "twitter", "twitter", "twitter"],
2: ["twitter", "twitter", "", "twitter", "", "", "", "", "", "", "", "twitter", "", "", "twitter", "twitter", "twitter", "twitter", "twitter", "twitter", "twitter", "twitter", "twitter", "twitter"]
]
extension ViewController: UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
func numberOfSections(in collectionView: UICollectionView) -> Int {
return nameArrays.keys.count
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return nameArrays.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell: IconCell = collectionView.dequeueReusableCell(withReuseIdentifier: cellName, for: indexPath) as! IconCell
let holdGesture = UILongPressGestureRecognizer()
holdGesture.addTarget(self, action: #selector(longPress))
holdGesture.minimumPressDuration = 0.5
cell.addGestureRecognizer(holdGesture)
cell.backgroundColor = .clear
// cell.appIcon.image = UIImage.init(named: nameArrays.values[indexPath.section][indexPath.row])
cell.appName.text = nameArrays.values[0][0]
return cell
}
答案 0 :(得分:0)
首先,所有数据源效率都不高,建议您使用结构或类
不过,您可以考虑各个部分并保留顺序来访问值
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
let key = nameArrays.keys.sorted()[section]
return nameArrays[key]!.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell: IconCell = collectionView.dequeueReusableCell(withReuseIdentifier: cellName, for: indexPath) as! IconCell
...
let key = nameArrays.keys.sorted()[indexPath.section]
cell.appName.text = nameArrays[key]![indexPath.item]
...
}