2017-07-24 26 views
1

我在一個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 
} 
+2

您需要在屬性中存儲對集合視圖的引用,而不是局部變量。這樣,您可以在數據源方法 – Paulw11

+0

@ Paulw11中對其進行比較,感謝您的評論,它有助於重新評估我的方法 – K1llarney

回答

1

不幸的是,當我去寫我的if語句時,xCode沒有找到我的集合視圖,即不會自動找到它們,因此我可以編譯。此外,我還爲我的手機收到了「未解決的標識符」。

這是因爲你還沒有定義全局收藏視圖。你已經在你的本地setupCollectionView裏面定義了它們,這就是爲什麼你不能在自動完成中找到它。

+0

不,收集視圖在全局範圍內* not *定義。它們是在'setupCollectionView'內部本地定義的。 – rmaddy

+0

@rmaddy我錯過了'不'。感謝您指出。道歉!! – luckyShubhra

+0

@luckyShubhra,謝謝你的輸入。我重新評估了我如何去做這件事。 – K1llarney

0

以下是您可以實現的方法。嘗試做向下轉換你的方法是這樣

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

if let collectionView = detailView as? CollectionViewB { 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellAID, for: indexPath) as! ListCell 
} else { 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellBID, for: indexPath) as! DetailCell 
    } 
    return cell 
} 

上面會只有當你有兩個UICollectionView網點

  • 另一個好辦法就是你可以拖動和您的廈門國際銀行 下降2個UIView的工作然後加載兩個UIViewController類,每個UIViewController類都有獨立的 UICollectionView對象。無論您首先找出實施方案的最佳方式。只是想拋出一些光。
1

謝謝所有評論。這已通過創建我的簡歷作爲全局惰性var封閉解決。

這使我可以在CV dataSource方法中訪問它們,併爲我的cellForItemAt指定不同的引用,從而允許在一個viewController中顯示兩個CV。

lazy var listCV: UICollectionView = { 

    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) 
    listView.delegate = self 
    listView.dataSource = self 

    listView.backgroundColor = UIColor.cyan 

    return listView 
}() 

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { 

if collectionView == listCV { 
    let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: listHeaderID, for: indexPath) as! HeaderCell 
    return header  
    } else { ... 
    } 
} 
相關問題