I’m trying to build multiple section uicollectionview in the same view the first collection view working fine but when I added the second one the app crashed (lldb),
both are custom cells define a suprate classes i used if statement as showing in this Answer: https://stackoverflow.com/a/44692356/8386142?stw=2 the code is clear no errors it just crashed like there is something wrong with constraints (lldb)
here is the code that I used to build it the
import UIKit
struct areaCustomCellData {
var lable: String
}
struct storesCustomCellData {
var lable: String
}
class Search: UIViewController {
let data = [
areaCustomCellData(lable: "شرق النيل"),
areaCustomCellData(lable: "بحري"),
areaCustomCellData(lable: "امدرمان"),
areaCustomCellData(lable: "الخرطوم"),
]
let storesData = [
storesCustomCellData(lable: "شرق النيل"),
storesCustomCellData(lable: "بحري"),
storesCustomCellData(lable: "امدرمان"),
storesCustomCellData(lable: "الخرطوم"),
]
let areaCView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.translatesAutoresizingMaskIntoConstraints = false
cv.register(customCell.self, forCellWithReuseIdentifier: "areaCell")
cv.backgroundColor = UIColor(red: 245, green: 245, blue: 245, a: 1)
cv.showsHorizontalScrollIndicator = false
return cv
}()
let storesCView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.translatesAutoresizingMaskIntoConstraints = false
cv.register(customCell2.self, forCellWithReuseIdentifier: "storesCell")
cv.backgroundColor = UIColor(red: 245, green: 245, blue: 245, a: 1)
cv.showsVerticalScrollIndicator = false
return cv
}()
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor(red: 245, green: 245, blue: 245, a: 1)
view.addSubview(areaCView)
view.addSubview(storesCView)
storesCView.delegate = self
storesCView.dataSource = self
areaCView.delegate = self
areaCView.dataSource = self
setUpLayout()
setUpNavBar()
setUpSearchBar()
}
func setUpLayout(){
areaCView.topAnchor.constraint(equalTo: view.topAnchor, constant: 120).isActive = true
areaCView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
areaCView.trailingAnchor.constraint(equalTo: view.trailingAnchor,constant: -20).isActive = true
areaCView.heightAnchor.constraint(equalToConstant: 30).isActive = true
storesCView.topAnchor.constraint(equalTo: view.topAnchor, constant: 500).isActive = true
storesCView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
storesCView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
storesCView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
}
extension Search: UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if indexPath.section == 0 {
return CGSize(width: collectionView.frame.width/4, height: collectionView.frame.height)
}else {
return CGSize(width: collectionView.frame.width/4, height: collectionView.frame.height)
}
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 2
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return (section == 0) ? data.count : storesData.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt
indexPath: IndexPath) -> UICollectionViewCell {
if indexPath.section == 0 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "areaCell", for: indexPath) as! customCell
cell.data = self.data[indexPath.row]
cell.backgroundColor = UIColor.white
cell.layer.cornerRadius = 10
return cell
}else{
let cell2 = collectionView.dequeueReusableCell(withReuseIdentifier: "storesCell", for: indexPath) as! customCell2
cell2.storesData = self.storesData[indexPath.row]
cell2.backgroundColor = UIColor.white
cell2.layer.cornerRadius = 10
return cell2
}
}
}
class customCell: UICollectionViewCell {
var data: areaCustomCellData? {
didSet{
guard let data = data else {return}
areaLable.text = data.lable
}
}
let areaLable: UILabel = {
let lable = UILabel()
lable.text = "الخرطوم"
lable.font = .systemFont(ofSize: 12)
lable.contentMode = .scaleAspectFit
lable.clipsToBounds = true
lable.textAlignment = .center
lable.translatesAutoresizingMaskIntoConstraints = false
return lable
}()
override init(frame: CGRect) {
super.init(frame: frame)
contentView.addSubview(areaLable)
areaLable.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
areaLable.leadingAnchor.constraint(equalTo: contentView.leadingAnchor).isActive = true
areaLable.trailingAnchor.constraint(equalTo: contentView.trailingAnchor).isActive = true
areaLable.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
class customCell2: UICollectionViewCell {
var storesData: storesCustomCellData? {
didSet{
guard let storesData = storesData else {return}
storesLable.text = storesData.lable
}
}
let storesLable: UILabel = {
let lable = UILabel()
lable.text = "الخرطوم"
lable.font = .systemFont(ofSize: 12)
lable.contentMode = .scaleAspectFit
lable.clipsToBounds = true
lable.textAlignment = .center
lable.translatesAutoresizingMaskIntoConstraints = false
return lable
}()
override init(frame: CGRect) {
super.init(frame: frame)
contentView.addSubview(storesLable)
storesLable.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
storesLable.leadingAnchor.constraint(equalTo: contentView.leadingAnchor).isActive = true
storesLable.trailingAnchor.constraint(equalTo: contentView.trailingAnchor).isActive = true
storesLable.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
Go to Source
Author: mazenqp