Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 一个Viewcontroller中的两个CollectionView_Swift_Uiviewcontroller_Uicollectionview - Fatal编程技术网

Swift 一个Viewcontroller中的两个CollectionView

Swift 一个Viewcontroller中的两个CollectionView,swift,uiviewcontroller,uicollectionview,Swift,Uiviewcontroller,Uicollectionview,我试图将两个CollectionView放在一个Viewcontroller中。我尝试过很多解决方案,但每次数据都只显示在第一个CollectionView中。我想放在两个CollectionView中的数据是相同的 我该怎么做 我的代码 import UIKit import Firebase import MobileCoreServices import AVKit private let reuseIdentifier = "Cell" var databaseRefRoom: FI

我试图将两个CollectionView放在一个Viewcontroller中。我尝试过很多解决方案,但每次数据都只显示在第一个CollectionView中。我想放在两个CollectionView中的数据是相同的

我该怎么做

我的代码

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) {

    }


}

让我们了解一下代码的问题是什么,以及如何解决它

第一名:

你提到:

我试图将两个CollectionView放在一个Viewcontroller中

但是您的代码似乎不包含两个集合视图。所以你应该做的是:

class ViewController: UIViewController {
    .
    .
    .

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

    .
    .
    .
}
确保将两个集合视图都连接到视图控制器

秒:

我尝试了很多解决方案,但每次数据都只显示在 第一个集合视图。我想在这两个CollectionView中放置的数据 是一样的

执行第一步后,确保符合集合视图
dataSource
delegate

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

        .
        .
        .
    }

    .
    .
    .
}
这将导致实现“我想在两个集合中放置的视图相同”的要求

同时

如果需要让每个集合视图从不同的数据源读取,该怎么办?通过设置集合视图的名称,可以让dateSource/delegate方法识别集合视图,如下所示:

viewDidLoad()方法中:

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

等等


希望这有帮助。

您只制作了一个作为
IBOutlet的collectionView
,第二个呢?它还应该作为一个
IBOutlet
连接到视图控制器……您的代码没有显示两个集合视图。我已经删除了上面代码中的第二个集合视图,因为我需要帮助来实现第二个感谢,我在第一次尝试时就这样做了。我刚刚发现了问题。但是谢谢anyway@CarstenHøvsgaard很乐意帮忙。顺便说一句,请允许我告诉你,我检查了你的个人资料,发现你不接受答案;接受答案的结果是:给你+2分,给回答者+15分,最重要的是它向观众表明这个问题的答案是正确的。这并不意味着我的答案对你的问题是可以接受的,但我认为这是为了提醒你接受答案:)
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
}