Reputation: 841
I am creating a collectionview as below
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
}()
Then using extension to the viewcontroller for the delegates
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)
}
}
I added the Collectionview to the in Viewdidload with constraints for the CollectionView
I could see purple collectionview but i could not see the Collectionview cell. Even when I add breakpoint to the UICollectionViewDataSource delegate it is coming to number of items but not coming to cellForItemAt indexpath
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))
Upvotes: 2
Views: 55
Reputation: 1061
Extending my comment, ensure that your layout delegate matches the layout:
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
}
Upvotes: 1