我有一个UICollectionView
嵌套在另一个ParentCollectionView
中,问题是当我向其设置数据时,数据变得混乱。节标题是按顺序设置的,但是嵌套的集合视图不是按顺序的。
这是代码
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()
}
}
最佳答案
解决了
说明:问题是我在情节提要中将collectionView
的高度设置为“ 100”,因为无论如何我稍后都会动态设置它(在collectionView
加载之后),但是,这导致未加载视图的行为很奇怪reloadData()
时正确显示。我猜想操作系统试图做的只是关心屏幕上可见的部分。
第二个问题是由于某种原因(我不知道),我必须使用numberOfSection
作为参数(从先前称为reloadData的参数中获取参数)来重新加载各个部分,我不知道为什么需要重新加载每个部分(我很想投票支持可以解释它的人)
简单:collectionView
取决于高度,高度取决于collectionView
。
解决方案:我不确定这是否是正确的方法,但是它对我有用,我将collectionView的高度更改为9000或任何较大的数字,因此操作系统知道它需要加载整个内容,并在调用校正高度。
self.parentCollectionView.reloadData()
let range = Range(uncheckedBounds: (0, self.parentCollectionView.numberOfSections))
let indexSet = IndexSet(integersIn: range)
self.parentCollectionView.reloadSections(indexSet)
let height = (self.parentCollectionView.collectionViewLayout.collectionViewContentSize.height)
self.collectionViewHeight.constant = height
关于ios - Swift CollectionView排序困惑,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45193556/