我有一个UICollectionView
嵌套在另一个ParentCollectionView
,问题是当我设置数据到它的数据变得混乱。部分标题按顺序设置,但嵌套的集合视图不按顺序。 下面的代码Swift CollectionView排序障碍
import UIKit
class HomeView: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
@IBOutlet weak var parentCollectionView: UICollectionView!
@IBOutlet weak var collectionViewHeight: NSLayoutConstraint!
let viewModel = HomeVM()
var categories = [ServiceSection]()
var categoriesReversed = [[String:Any]]()
var collectionViewTags = Int()
override func viewDidLoad() {
super.viewDidLoad()
self.categories = [ServiceSection(
section:
["id":"2","name":"planet","service":[
["id":"1","name":"Sun"]
]
]
),
ServiceSection(
section:
["id":"2","name":"Animal","service":[
["id":"1","name":"Dog"]
]
])
,
ServiceSection(
section:
["id":"2","name":"Daries","service":[
["id":"1","name":"Milk"]
]
])
,
ServiceSection(
section:
["id":"2","name":"Meet","service":[
["id":"1","name":"Steak"]
]
])
]
let height = (self.parentCollectionView.collectionViewLayout.collectionViewContentSize.height)
self.collectionViewHeight.constant = height
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
if (collectionView == parentCollectionView)
{
return self.categories.count
}
else
{
collectionView.tag = collectionViewTags
collectionViewTags += 1
return 1
}
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if (collectionView == parentCollectionView){
return 1
}else{
let section = self.categories[collectionView.tag].services
return section.count
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if (collectionView == parentCollectionView){
return collectionView.dequeueReusableCell(withReuseIdentifier: "parentCell", for: indexPath)
}
else
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionCell
let collection = self.categories[collectionView.tag].services
let cellData = collection[indexPath.row]
cell.title.text = cellData.name
return cell
}
}
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
if collectionView == parentCollectionView{
return CGSize(width: parentCollectionView.frame.width, height: 30)
}
return CGSize(width:150,height:80)
}
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
if parentCollectionView == collectionView {
let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "header", for: indexPath) as! CollectionHeader
header.header.text = self.categories[indexPath.section].name
return header
}
return UICollectionReusableView()
}
}
您需要按什么命令? –
按照它们在数据源中的顺序排序。请查看章节标题和章节内容,并将它们与它们不匹配的数据源进行比较 – moawaya