【问题标题】:Adding a Second CollectionView添加第二个 CollectionView
【发布时间】:2022-06-10 19:46:47
【问题描述】:

在尝试添加第二个 CollectionView 时,我迷路了。 Here 是我未来的项目,我实际上是在尝试复制它(第二个 collectionView 的原因是我将有 4 行,但前两个和下两个将独立滚动)。

Here 是故事板供参考。

然而我得到了这个错误here(第二张照片):here

这是我最初的 ViewController (WORKING) 的代码

后跟SecondViewController代码,导致app显示上面的消息。

  import UIKit

  class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {


@IBOutlet var collectionViewButtons: UICollectionView!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    collectionViewButtons.delegate = self
    collectionViewButtons.dataSource = self
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 6 //number of buttons
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! ButtonCollectionViewCell
    
    cell.buttonLive.setTitle("Handling a Breakup", for: .normal) //set button title
cell.buttonLive.titleLabel!.font = UIFont(name: "Marker Felt", size: 20)
    cell.buttonLive.layer.cornerRadius = 10
    cell.buttonLive.clipsToBounds = true
    cell.buttonLive.layer.borderWidth = 1.0
    cell.buttonLive.layer.borderColor = UIColor.white.cgColor

    if indexPath.item == 0 { //first button
        cell.buttonLive.backgroundColor = UIColor.darkGray //set button background
    }
    else if indexPath.item == 1 { //second button
        cell.buttonLive.backgroundColor = UIColor.systemGray
        cell.buttonLive.setTitle("Good Work", for: .normal)

    }
    else if indexPath.item == 2 { //3rd button
        cell.buttonLive.backgroundColor = UIColor.darkGray
    }
    else { // for remaining buttons
        cell.buttonLive.backgroundColor = UIColor.darkGray
    }
    
    return cell
}
   func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    if indexPath.item == 0 { // opens any page by clicking button 1
  //      let vc = storyboard?.instantiateViewController(withIdentifier: "anyVC1") as! ViewController1
//        navigationController?.pushViewController(vc, animated: true)
      //      }
    //      else if indexPath.item == 1 {
    //          let vc = storyboard?.instantiateViewController(withIdentifier: "anyVC2") as! ViewController2
  //      navigationController?.pushViewController(vc, animated: true)
    }
    //      else if indexPath.item == 2 {
    //          let vc = storyboard?.instantiateViewController(withIdentifier: "anyVC3") as! ViewController3
   //          navigationController?.pushViewController(vc, animated: true)
         }
         //      else {
         //         let vc = storyboard?.instantiateViewController(withIdentifier: "anyVC4") as! ViewController4
          //      navigationController?.pushViewController(vc, animated: true)
    }
    //  }

    //}
    // You can return any number of buttons by changing return 6 to any required num

第二视图控制器:

    import UIKit


    class SecondViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

override func viewDidLoad() {
    super.viewDidLoad()

        // Do any additional setup after loading the view.

    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 6 //number of buttons
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let SecondCell = collectionView.dequeueReusableCell(withReuseIdentifier: "SecondCell", for: indexPath) as! ButtonCollectionViewCell
        
        SecondCell.buttonTwo.setTitle("Handling a Breakup", for: .normal) //set button title
    SecondCell.buttonLive.titleLabel!.font = UIFont(name: "Marker Felt", size: 20)
        SecondCell.buttonTwo.layer.cornerRadius = 10
        SecondCell.buttonTwo.clipsToBounds = true
        SecondCell.buttonTwo.layer.borderWidth = 1.0
        SecondCell.buttonTwo.layer.borderColor = UIColor.white.cgColor

        if indexPath.item == 0 { //first button
            SecondCell.buttonTwo.backgroundColor = UIColor.darkGray //set button background
        }
        else if indexPath.item == 1 { //second button
            SecondCell.buttonTwo.backgroundColor = UIColor.systemGray
            SecondCell.buttonTwo.setTitle("Good Work", for: .normal)

        }
        else if indexPath.item == 2 { //3rd button
            SecondCell.buttonTwo.backgroundColor = UIColor.darkGray
        }
        else { // for remaining buttons
            SecondCell.buttonTwo.backgroundColor = UIColor.darkGray
        }
        
        return SecondCell
    }
       func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if indexPath.item == 0 { // opens any page by clicking button 1
      //      let vc = storyboard?.instantiateViewController(withIdentifier: "anyVC1") as! ViewController1
    //        navigationController?.pushViewController(vc, animated: true)
  //      }
  //      else if indexPath.item == 1 {
  //          let vc = storyboard?.instantiateViewController(withIdentifier: "anyVC2") as! ViewController2
      //      navigationController?.pushViewController(vc, animated: true)
        }
  //      else if indexPath.item == 2 {
  //          let vc = storyboard?.instantiateViewController(withIdentifier: "anyVC3") as! ViewController3
  //          navigationController?.pushViewController(vc, animated: true)
        }
  //      else {
   //         let vc = storyboard?.instantiateViewController(withIdentifier: "anyVC4") as! ViewController4
      //      navigationController?.pushViewController(vc, animated: true)
        }
  //  }
    
//}
// You can return any number of buttons by changing return 6 to any required num

注意事项: 我也经历过并没有成功: 将所有“collectionView”文字更改为“SecondCollection”,因为这是我的第二个 collectionView 的名称。

我已经为两个collectionView 设置了一个Collection IBOutlet。 我为两个按钮设置了单独的 IBOutlet。

【问题讨论】:

  • 这很令人困惑...您是否要在一个视图上显示两个水平滚动的集合视图(在一个视图控制器中)?
  • @DonMag 我想在我的应用程序中有两个水平滚动视图。 (将来会有更多的水平滚动集合视图)。我添加了第二个 collectionView 并为它创建了一个文件并尝试编辑但失败了,这就是您看到的代码“第二个视图控制器”

标签: swift xcode uicollectionview swift5 xcode13


【解决方案1】:

如果您需要两个(或更多)集合视图,则不需要两个控制器...您需要检查哪个集合视图正在请求数据(或正在与之交互)并返回适当的信息。

因此,您的课程将如下所示:

class TwoCollectionsViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
    
    @IBOutlet var firstCV: UICollectionView!
    @IBOutlet var secondCV: UICollectionView!

    let firstData: [String] = [
        "Btn 1", "Btn 2", "Btn 3", "Btn 4", "Btn 5",
    ]
    let secondData: [String] = [
        "Second 1", "Second 2", "Second 3", "Second 4"
    ]

    override func viewDidLoad() {
        super.viewDidLoad()
        
        firstCV.dataSource = self
        firstCV.delegate = self
        
        secondCV.dataSource = self
        secondCV.delegate = self
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        // if it's the First Collection View
        if collectionView == firstCV {
            return firstData.count
        }

        // it's not the First Collection View, so it's the Second one
        return secondData.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        // if it's the First Collection View
        if collectionView == firstCV {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "firstCell", for: indexPath) as! FirstCollectionViewCell
            cell.buttonOne.setTitle(firstData[indexPath.item], for: [])
            return cell
        }
        
        // it's not the First Collection View, so it's the Second one
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "secondCell", for: indexPath) as! SecondCollectionViewCell
        cell.buttonTwo.setTitle(secondData[indexPath.item], for: [])
        return cell

    }

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

        // if it's the First Collection View
        if collectionView == firstCV {
            // do what you want because a cell in the First Collection View was selected
            return
        }
        
        // it's not the First Collection View, so it's the Second one
        // do what you want because a cell in the Second Collection View was selected
        
    }
    
}

【讨论】:

  • 我已经尝试了这段代码,但我确实遇到了:“这个类不符合 key firstCV 的键值编码。”我已经检查了所有的 IBconnections,以及文件类型或代码中的任何其他错误。我在第二个 UICollectionViewCell 文件上有两个按钮(因为有两个集合视图)。有什么想法吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-31
  • 2021-09-11
  • 2022-01-18
  • 1970-01-01
  • 1970-01-01
  • 2015-12-16
相关资源
最近更新 更多