【问题标题】:UICollectionView must be initialized with a non-nil layout parameter'UICollectionView 必须使用非零布局参数初始化'
【发布时间】:2016-10-29 13:26:26
【问题描述】:

我试图在视图控制器中使用两个集合视图,结果收到此错误消息?你能指出我缺少什么来纠正这个问题吗?提前致谢.. 下面是我在名为“OtherViewController”的视图控制器中的代码。

类 OtherViewController : UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {

//1. CREATE A VARIABLE FOR YELLOW ARRAY SO THAT I CAN DEFINE A STRING
var exploreArray = [String]()
var exploreHeader = [String]()
var yellowImages = [String]()
var explorecategories = [String]()


@IBOutlet weak var mycollectionView: UICollectionView!


var collectionViewA = UICollectionView()
let collectionViewB = UICollectionView()
let collectionViewAIdentifier = "yellowCell"
let collectionViewBIdentifier = "yellowCell2"

 override func viewDidLoad() {
    super.viewDidLoad()


    // Do any additional setup after loading the view.

        collectionViewA.delegate = self
        collectionViewB.delegate = self

        collectionViewA.dataSource = self
        collectionViewB.dataSource = self


        self.view.addSubview(collectionViewA)
        self.view.addSubview(collectionViewB)

}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

   if collectionView == self.collectionViewA {
         return exploreArray.count        }

    return 1 // Replace with count of your data for collectionViewB

} 
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
   if collectionView == self.collectionViewA{

        let cellA = collectionView.dequeueReusableCell(withReuseIdentifier: "yellowCell", for: indexPath) as! yellowCell
                // Set up cell

    //DISPLAY TITLE LABEL
    cellA.yLabel.text = exploreHeader[indexPath.row]

    //DISPLAY IMAGES
    cellA.yellowImages.image  = UIImage(named:yellowImages [indexPath.row])

    //DISPLAY DESCRIPTION
    cellA.yellowTextField.text = exploreArray [indexPath.row]

    return cellA
    }

    else {

       let cellB =  collectionView.dequeueReusableCell(withReuseIdentifier: "yellowCell2", for: indexPath) as! yellowCell2

    cellB.labe2.text = "Dolphin"

//只是随机标签来查看cellB是否会出现在collection view中

               return cellB
    }

    }

【问题讨论】:

    标签: uicollectionviewlayout


    【解决方案1】:

    我认为我对this question 的回答也可能对您的情况有所帮助。

    具体来说,你在哪里

    var collectionViewA = UICollectionView()
    let collectionViewB = UICollectionView()
    

    建议(有几个比我聪明得多的人!)改用它

    var collectionViewA: UICollectionView!
    var collectionViewB: UICollectionView!
    

    然后在 viewDidLoad() 中,连同设置委托和数据源(如您所做的那样),添加类似

    let layoutA = UICollectionViewFlowLayout()                  
    layoutA.itemSize = CGSize(width: 100, height: 100)
    
    let layoutB = UICollectionViewFlowLayout()
    layoutB.itemSize = CGSize(width: 100, height: 100)
    
    collectionViewA = UICollectionView(frame: self.view.frame, collectionViewLayout: layoutA)
    collectionViewB = UICollectionView(frame: self.view.frame, collectionViewLayout: layoutB)
    

    最后在添加子视图之后,

    collectionViewLeft.register(UICollectionViewCell.self, forCellWithReuseIdentifier: collectionViewLeftIdentifier)
    collectionViewRight.register(UICollectionViewCell.self, forCellWithReuseIdentifier: collectionViewRightIdentifier)
    

    完整代码显示在链接问题的答案中。 (使用 left & right 而不是 A & B,但希望同样的想法适用。)

    【讨论】:

      猜你喜欢
      • 2014-08-08
      • 2019-09-16
      • 1970-01-01
      • 1970-01-01
      • 2018-05-09
      • 1970-01-01
      • 2019-01-19
      • 1970-01-01
      • 2017-04-08
      相关资源
      最近更新 更多