添加第二个集合视图



在我尝试添加第二个CollectionView时,我已经丢失了。这是我未来的项目,我基本上试图复制(第二个collectionView的原因是,我将有4行,但顶部两个和底部两个将独立滚动)。

这是供参考的故事板。

然而,我在这里得到这个错误(第二张照片):这里

这是我的代码为原来的ViewController (WORKING)

后面是SecondViewController代码,它导致应用程序显示上面的消息。

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"写作要说"第二集";因为这就是我的第二个collectionView的名字

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

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

那么,你的类看起来就像这样:

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

}

}

最新更新