Ios 未调用UICollectionView方法(collectionView cellForItemAt indexPath)
我试图使用Ios 未调用UICollectionView方法(collectionView cellForItemAt indexPath),ios,swift,xcode,uicollectionview,uicollectionviewcell,Ios,Swift,Xcode,Uicollectionview,Uicollectionviewcell,我试图使用UICollectionView创建一个自定义的条形菜单,问题是没有调用cells not show方法collectionView cellForItemAt indexPath 到目前为止,我的代码是: import UIKit private let reuseIdentifier = "Cell" class MenuBar : UIView ,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionVi
UICollectionView
创建一个自定义的条形菜单,问题是没有调用cells not show方法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")
}
}
请检查:
您的布局应该如下所示
您使用了UICollectionViewLayout
您必须使用UICollectionViewFlowLayout
let layout = UICollectionViewFlowLayout()
请检查:
您的布局应该如下所示
您使用了UICollectionViewLayout
您必须使用UICollectionViewFlowLayout
let layout = UICollectionViewFlowLayout()
看起来数据源有问题 您的菜单栏位于视图控制器上,对吗 您不能将collectionView的数据源设置为菜单栏(委托也设置为)。您可以将collectionView的数据源设置为视图控制器
希望此建议能对您有所帮助。看起来数据源有问题 您的菜单栏位于视图控制器上,对吗 您不能将collectionView的数据源设置为菜单栏(委托也设置为)。您可以将collectionView的数据源设置为视图控制器
希望此建议能对您有所帮助。UICollectionView的框架是否可见(您可以查看)。您可能需要调用一个
reloadData()
。是的UICollectionIIoview是可见的我选中了它是UICollectionView的可见框架(您可以使用进行检查)。您可能需要调用一个reloadData()
。是的UICollectiView可见我检查了两个小时,通过扫描示例项目中的每个字符来确定coll.view不工作的原因。谢谢我花了两个小时来确定我的coll.view为什么不工作,确切地说是扫描样本项目中的每个字符。谢谢