【问题标题】:UICollectionView method (collectionView cellForItemAt indexPath ) not called未调用 UICollectionView 方法(collectionView cellForItemAt indexPath)
【发布时间】:2017-09-29 21:55:27
【问题描述】:

我正在尝试使用UICollectionView 制作自定义栏菜单,问题是单元格未显示方法collectionView cellForItemAt indexPath 未调用。

到目前为止我的代码:

import UIKit
private let reuseIdentifier = "Cell"

class MenuBar : UIView ,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{

   lazy var collectionView : UICollectionView = {
        let layout = UICollectionViewLayout()
        let cv = UICollectionView(frame: .zero , collectionViewLayout : layout)
        cv.dataSource = self
        cv.delegate = self
        cv.backgroundColor = UIColor.white
        return cv
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        addSubview(collectionView)
        setUpCollectionViewConstraints()
    }

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

     func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)
        cell.backgroundColor = UIColor.red

        return cell
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: frame.height/2, height: frame.height)
    }

    fileprivate func setUpCollectionViewConstraints() {
        collectionView.translatesAutoresizingMaskIntoConstraints = false
        let horizontalConstraints = NSLayoutConstraint.constraints(withVisualFormat: "H:|[cv]|", options: NSLayoutFormatOptions.alignAllCenterY, metrics: nil, views: ["cv" : collectionView])
        let verticalConstraints = NSLayoutConstraint.constraints(withVisualFormat:"V:|[cv]|", options: NSLayoutFormatOptions.alignAllCenterY, metrics: nil, views: ["cv" : collectionView])
        addConstraints(horizontalConstraints)
        addConstraints(verticalConstraints)
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

【问题讨论】:

标签: ios swift xcode uicollectionview uicollectionviewcell


【解决方案1】:

请检查:

您的布局应该如下所示。

你使用了UICollectionViewLayout

你必须使用UICollectionViewFlowLayout

let layout = UICollectionViewFlowLayout()

【讨论】:

  • 我花了两个小时通过扫描示例项目中的每个字符来确定我的 coll.view 无法正常工作的原因。谢谢!
【解决方案2】:

数据源似乎有问题。

您的 MenuBar 位于 ViewController 上,对吗?

您不能将 collectionView 的 datasource 设置为 MenuBarDelegate 也是如此)。您可以将 collectionView 的 datasource 设置为 ViewController

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多