UICollectionView ব্যবহার করতে DataSource, Delegate এবং অপশনালি DelegateFlowLayout কনফর্ম করুন।
প্রোগ্রাম্যাটিক্যালি কালেকশন ভিউ তৈরি —
func initCollection() {
let layout = UICollectionViewFlowLayout()
layout.itemSize = CGSize(width: 50, height: 50)
let collection = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
collection.dataSource = self
collection.delegate = self
collection.backgroundColor = .systemGreen
collection.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell")
self.view.addSubview(collection)
}
DataSource মেথড —
func numberOfSections(in collectionView: UICollectionView) -> Int { return 1 }
func collectionView(_ collection: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 7 }
func collectionView(_ collection: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collection.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
cell.layer.backgroundColor = UIColor.systemRed.cgColor
return cell
}
অপশনাল: ডাইনামিক সেল সাইজ (DelegateFlowLayout) —
func collectionView(_ collection: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 200, height: 50)
}
ফলাফল —
