我正在创建如下的collectionview
lazy var testCollectionView: UICollectionView = {
let collectionViewLayout = UICollectionViewLayout();
let testCollectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: collectionViewLayout)
testCollectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell")
testCollectionView.translatesAutoresizingMaskIntoConstraints = false
testCollectionView.backgroundColor = UIColor.purple
testCollectionView.dataSource = self
testCollectionView.delegate = self
return testCollectionView
}()
然后使用代表的viewcontroller扩展名
extension ViewController: UICollectionViewDataSource{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
print("jjjjjjjj")
return 10
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
print("vjvjvjvhvjvjhvgh")
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
cell.contentView.backgroundColor = UIColor.magenta
return cell
}
}
extension ViewController : UICollectionViewDelegate{
}
extension ViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
print("sdfsf")
return CGSize(width: collectionView.bounds.size.width - 16, height: 120)
}
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
minimumLineSpacingForSectionAt section: Int) -> CGFloat {
print("sdsdsdsdsdsd")
return 8
}
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets.init(top: 8, left: 8, bottom: 8, right: 8)
}
}
我将Collectionview添加到Viewdidload中,其中包含CollectionView的约束
我可以看到紫色的collectionview,但看不到Collectionview单元格。即使将断点添加到UICollectionViewDataSource委托中,它也会涉及到项目数,但不会涉及到cellForItemAt索引路径
self.view.addSubview(testCollectionView)
self.view.addConstraint(NSLayoutConstraint(item: testCollectionView, attribute: .width , relatedBy: .equal, toItem: self.view, attribute: .width, multiplier: 0.7, constant: 0.0))
self.view.addConstraint(NSLayoutConstraint(item: testCollectionView, attribute: .height, relatedBy: .equal, toItem: self.view, attribute: .height, multiplier: 0.7, constant: 0.0))
self.view.addConstraint(NSLayoutConstraint(item: testCollectionView, attribute: .centerY, relatedBy: .equal, toItem: self.view, attribute: .centerY, multiplier: 1.0, constant: 0.0))
self.view.addConstraint(NSLayoutConstraint(item: testCollectionView, attribute: .centerX, relatedBy: .equal, toItem: self.view, attribute: .centerX, multiplier: 1.0, constant: 0.0))
答案 0 :(得分:1)
在扩展我的评论之前,请确保您的布局委托与布局匹配:
lazy var testCollectionView: UICollectionView = {
let collectionViewLayout = UICollectionViewFlowLayout()
let collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: collectionViewLayout)
// further setup
return collectionView
}()
extension ViewController: UICollectionViewDelegateFlowLayout {
// Implement delegate functions as necessary
}