Hi I am building a simple multi column CollectionView here and trying to add 3 cell in a row. Well, in short the column and rows looks all fine if I comment the below code. which is no image and text assigned to cells.
let framework = list[indexPath.item] cell.configure(framework)
However, once uncommented, only a single cell is displayed per row and the image doesn't seem to be resizing automatically.
Can you please advise.
Below is the ViewController.
import UIKit
class FrameworkListViewController: UIViewController {
@IBOutlet weak var collectionView: UICollectionView!
let list: [AppleFramework] = AppleFramework.list
override func viewDidLoad() {
super.viewDidLoad()
collectionView.dataSource = self
collectionView.delegate = self
}
}
extension FrameworkListViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return list.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "FrameworkCell", for: indexPath) as? FrameworkCell else {
return UICollectionViewCell()
}
let framework = list[indexPath.item]
cell.configure(framework)
return cell
}
}
extension FrameworkListViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let interItemSpacing: CGFloat = 10
let width = (collectionView.bounds.width - interItemSpacing * 2) / 3
let height = width * 1.5
return CGSize(width: width, height: height)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 10
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 10
}
}
This is Cell
class FrameworkCell: UICollectionViewCell {
@IBOutlet weak var thumbnailImageView: UIImageView!
@IBOutlet weak var nameLabel: UILabel!
func configure(_ framework: AppleFramework) {
thumbnailImageView.image = UIImage(named: framework.imageName)
nameLabel.text = framework.name
}
}