2017-05-25 23 views
-1

我試圖將兩個CollectionViews放在一個Viewcontroller中。我嘗試了很多解決方案,但每次只在第一個集合視圖中顯示數據。我想在兩個CollectionView中輸入的數據都是一樣的。一個ViewController中的兩個CollectionViews

如何做到這一點

我的代碼

import UIKit 
import Firebase 
import MobileCoreServices 
import AVKit 

private let reuseIdentifier = "Cell" 

var databaseRefRoom: FIRDatabaseReference { 
    return FIRDatabase.database().reference() 
    } 

class RoomViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UITableViewDelegate { 

    //ScrollView 
    @IBOutlet weak var favoritesBtn: UIButton! 
    @IBOutlet weak var yourChatBtn: UIButton! 
    @IBOutlet weak var mostPopularBtn: UIButton! 

    //RoomCollectionView -> RoomViewCollectionViewCell 
    var rooms = [Room]() 
    @IBOutlet weak var collectionView: UICollectionView! 



    //RoomViewController material 
    override func viewDidLoad() { 
     super.viewDidLoad() 

     self.title = "Chuloo" 

     self.navigationController?.isNavigationBarHidden = false 

     favoritesBtn.setTitle("Favorites", for:.normal) 
     favoritesBtn.titleLabel?.textColor = UIColor.white 
     favoritesBtn.titleLabel?.font = UIFont(name: "AppleSDGothicNeo-Bold", size: 14) 
     favoritesBtn.backgroundColor = UIColor.orange 

     yourChatBtn.setTitle("Your Chat", for:.normal) 
     yourChatBtn.titleLabel?.textColor = UIColor.white 
     yourChatBtn.titleLabel?.font = UIFont(name: "AppleSDGothicNeo-Bold", size: 14) 
     yourChatBtn.backgroundColor = UIColor.red 

     mostPopularBtn.setTitle("Most Popular", for:.normal) 
     mostPopularBtn.titleLabel?.textColor = UIColor.white 
     mostPopularBtn.titleLabel?.font = UIFont(name: "AppleSDGothicNeo-Bold", size: 14) 
     mostPopularBtn.backgroundColor = UIColor.blue 


     //RoomCollectionView -> Display CollectionView i ScrollView -> Extension 
     collectionView.dataSource = self 
     collectionView.delegate = self 

     let date = Date() 
     let formatter = DateFormatter() 
     formatter.dateFormat = "dd.MMMM.yyyy - hh:mm:ss a" 
     formatter.amSymbol = "AM" 
     formatter.pmSymbol = "PM" 
     let result = formatter.string(from: date) 

     //Hide backButton 
     self.navigationItem.setHidesBackButton(true, animated: false) 

     //RoomCollectionView -> DataService fetch from Server 
     DataService.dataService.fetchDataFromServer { (room) in 
      self.rooms.append(room) 
      let indexPath = IndexPath(item: self.rooms.count - 1, section: 0) 
      self.collectionView?.insertItems(at: [indexPath]) 
      } 

     //Online User Status 
     let usersRef = databaseRefRoom.child("online") 
     let currentUserRef = usersRef.child((FIRAuth.auth()?.currentUser?.displayName)!) 
     currentUserRef.setValue("online") 
     currentUserRef.onDisconnectRemoveValue() 

     //Database User child Online Status 
     let usersRefUser = databaseRefRoom.child("users").child((FIRAuth.auth()?.currentUser?.displayName)!).child("Online Status").child("online") 
     usersRefUser.setValue(result) 

     let usersRefOffline = databaseRefRoom.child("users").child((FIRAuth.auth()?.currentUser?.displayName)!).child("Online Status") 
     usersRefOffline.onDisconnectUpdateChildValues(["offline": result]) 


    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 

    } 
} 

//RoomCollectionView -> Display 
extension RoomViewController { 

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return rooms.count 

    } 

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "roomCell", for: indexPath) as! RoomViewCollectionViewCell 

     let room = rooms[indexPath.row] 

     cell.layer.cornerRadius = 4 
     cell.layer.borderColor = UIColor(red: 248.0/255.0, green: 248.0/255.0, blue: 248.0/255.0, alpha: 1.0).cgColor 
     cell.layer.borderWidth = 1 
     cell.layer.shadowColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.25).cgColor 
     cell.layer.shadowOffset = CGSize(width: 0, height: 2) 
     cell.layer.shadowOpacity = 0.5 
     cell.layer.shadowRadius = 1.0 
     cell.layer.masksToBounds = false 

     // Configure the cell 
     cell.configureCell(room: room) 
     return cell 
    } 

    func collectionView(collectionView: UICollectionView, layout: UICollectionViewLayout, sizeForItemAt: IndexPath) -> CGSize { 
     return CGSize(width: view.frame.width/2 - 5, height: view.frame.width/2 - 5) 

    } 

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 

    } 


} 
+0

你只有一個collectionView作爲'IBOutlet',那麼第二個呢?它應該也連接到視圖控制器作爲'IBOutlet' ... –

+0

您的代碼不顯示兩個集合視圖。 – David

+0

我已經在上面的代碼中刪除了第二個collectionView,因爲我需要幫助來實現第二個 –

回答

0

讓我們通過什麼是你的代碼的問題,以及如何可以解決這個問題。

第一:

你提到:

我正嘗試將兩個CollectionViews在一個視圖控制器。

但您的代碼似乎不包含兩個集合視圖。所以你應該做的是:

class ViewController: UIViewController { 
    . 
    . 
    . 

    @IBOutlet weak var collectionView1: UICollectionView! 
    @IBOutlet weak var collectionView2: UICollectionView! 

    . 
    . 
    . 
} 

確保你連接兩個集合視圖到視圖控制器。

二:

我已經嘗試了多種解決方案,但每次的數據時,只顯示在 第一CollectionsView。我想要在CollectionView 中輸入的數據是相同的。

確保-after實施第一步驟 - 是符合收集意見dataSourcedelegate

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate { 
    . 
    . 
    . 

    @IBOutlet weak var collectionView1: UICollectionView! 
    @IBOutlet weak var collectionView2: UICollectionView! 

    override func viewDidLoad() { 
     . 
     . 
     . 

     collectionView1.dataSource = self 
     collectionView1.delegate = self 
     collectionView2.dataSource = self 
     collectionView2.delegate = self 

     . 
     . 
     . 
    } 

    . 
    . 
    . 
} 

這應該導致實現「我希望把兩者的CollectionView要求是一樣的「。

此外

如果你需要讓每個集合視圖中,從不同的數據源讀取?你可以讓dateSource /委託方法通過設置tag它來識別集合視圖,如下所示:

viewDidLoad()方法:

// setting tags: 
collectionView1.tag = 101 
collectionView2.tag = 102 

這樣:

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return collectionView === collectionView1 ? dataSource1.count : dataSource2.count 
} 

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CellID", for: indexPath) 

    let currentObject = collectionView === collectionView1 ? dataSource1[indexPath.row] : dataSource2[indexPath.row] 

    . 
    . 
    . 

    return cell 
} 

等等在...

希望這有助於。

+0

Thank's我是在第一次嘗試中做到的。我剛剛發現了這個問題。但是,無論如何謝謝 –

+0

@CarstenHøvsgaard很高興幫助。順便說一下,讓我注意你,我檢查了你的個人資料,並注意到你不接受答案;接受答案導致:給你+2分,給答案者+15分,最重要的是它給觀察者指示這個答案對這個問題是正確的。這並不意味着我的答案對於你的問題是可以接受的,但我認爲這將提醒你接受答案:) –

相關問題