我在一個ViewController中並排獲取兩個集合視圖時遇到了一些麻煩。Swift 3 - 多個CollectionViews - 編程式
我的問題是不建立或顯示集合視圖。單獨而言,我可以根據需要在屏幕上顯示兩者,但不能同時顯示。
我已經調試我的代碼,發現設置「cellForItemAt」
我還搜查了論壇和理解,我需要有一個if語句來說,當它落在了......
如果的CollectionView == 1,然後出列單元格A,否則出列單元格B
不幸的是,當我去寫if語句時,xCode沒有找到我的集合視圖,即不會自動找到它們,因此我可以編譯。此外,我還爲我的手機收到了「未解決的標識符」。
鑑於我上面的努力,我碰到了一堵牆,想知道是否有人能夠解釋我的問題?由於
的viewDidLoad代碼
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.isNavigationBarHidden = false
setupCollectionView()
}
的CollectionView創建代碼
func setupCollectionView() {
let listLayout = UICollectionViewFlowLayout()
listLayout.itemSize = CGSize(width: 300, height: 40)
listLayout.minimumLineSpacing = 5
listLayout.sectionHeadersPinToVisibleBounds = true
let listFrame = CGRect(x: 0, y: 64, width: 325, height: 500)
let listView = UICollectionView(frame: listFrame, collectionViewLayout: listLayout)
// cell
listView.register(cellA.self, forCellWithReuseIdentifier: cellAId)
// header
listView.register(headerCellA.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: headerAID)
listView.delegate = self
listView.dataSource = self
listView.backgroundColor = .cyan
self.view.addSubview(listView)
let detailLayout = UICollectionViewFlowLayout()
detailLayout.itemSize = CGSize(width: 300, height: 40)
detailLayout.minimumLineSpacing = 5
detailLayout.sectionHeadersPinToVisibleBounds = true
let detailFrame = CGRect(x: 330, y: 64, width: 500, height: 650)
let detailView = UICollectionView(frame: detailFrame, collectionViewLayout: detailLayout)
// cell
detailView.register(cellB.self, forCellWithReuseIdentifier: cellBID)
// header
detailView.register(headerCellB.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: headerBID)
detailView.delegate = self
detailView.dataSource = self
detailView.backgroundColor = .white
self.view.addSubview(detailView)
}
cellForItemAt碼 - 不能讓我的if語句工作
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if collectionView == detailView {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellAID, for: indexPath) as! ListCell
} else {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellBID, for: indexPath) as! DetailCell
}
return cell
}
您需要在屬性中存儲對集合視圖的引用,而不是局部變量。這樣,您可以在數據源方法 – Paulw11
@ Paulw11中對其進行比較,感謝您的評論,它有助於重新評估我的方法 – K1llarney