0

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][1] 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")
    }
}

crash log

Assertion failure in -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:viewCategory:], /Library/Caches/com.apple.xbs/Sources/UIKitCore_Sim/UIKit-3920.26.113/UICollectionView.m:5971
2020-05-26 06:49:07.303674+0200 DELEVARE - ديليفري[20979:1173492] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier areaCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000106b0cf0e __exceptionPreprocess + 350
    1   libobjc.A.dylib                     0x00007fff208a59b2 objc_exception_throw + 48
    2   CoreFoundation                      0x0000000106b0cc88 +[NSException raise:format:arguments:] + 88
    3   Foundation                          0x0000000104cb5cd2 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 191
    4   UIKitCore                           0x000000011055636e -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:viewCategory:] + 2426
    5   UIKitCore                           0x000000011055653d -[UICollectionView dequeueReusableCellWithReuseIdentifier:forIndexPath:] + 88
    6   DELEVARE - ديليفري           0x0000000101adb4e5 $s0025DELEVARE____gjoAaHtrbEfbc6SearchC14collectionView_13cellForItemAtSo012UICollectionC4CellCSo0hC0C_10Foundation9IndexPathVtF + 373
    7   DELEVARE - ديليفري           0x0000000101adbbe5 $s0025DELEVARE____gjoAaHtrbEfbc6SearchC14collectionView_13cellForItemAtSo012UICollectionC4CellCSo0hC0C_10Foundation9IndexPathVtFTo + 165
    8   UIKitCore                           0x00000001105409d5 -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:isFocused:notify:] + 416
    9   UIKitCore                           0x000000011054082f -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:] + 31
    10  UIKitCore                           0x00000001105460cf -[UICollectionView _updateVisibleCellsNow:] + 6402
    11  UIKitCore                           0x000000011054b21e -[UICollectionView layoutSubviews] + 351
    12  UIKitCore                           0x0000000111294848 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 2478
    13  QuartzCore                          0x0000000105e043f0 -[CALayer layoutSublayers] + 255
    14  QuartzCore                          0x0000000105e0a57b _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 523
    15  QuartzCore                          0x0000000105e15c12 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 80
    16  QuartzCore                          0x0000000105d5ec84 _ZN2CA7Context18commit_transactionEPNS_11TransactionEd + 324
    17  QuartzCore                          0x0000000105d9265f _ZN2CA11Transaction6commitEv + 649
    18  UIKitCore                           0x0000000110daac2b __34-[UIApplication _firstCommitBlock]_block_invoke_2 + 81
    19  CoreFoundation                      0x0000000106a70cdc __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
    20  CoreFoundation                      0x0000000106a703d3 __CFRunLoopDoBlocks + 195
    21  CoreFoundation                      0x0000000106a6b1c3 __CFRunLoopRun + 995
    22  CoreFoundation                      0x0000000106a6aac4 CFRunLoopRunSpecific + 404
    23  GraphicsServices                    0x000000010bbacc1a GSEventRunModal + 139
    24  UIKitCore                           0x0000000110d92f80 UIApplicationMain + 1605
    25  DELEVARE - ديليفري           0x0000000101ae046b main + 75
    26  libdyld.dylib                       0x00007fff212f61fd start + 1
    27  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

  [1]: https://stackoverflow.com/a/44692356/8386142?stw=2
mazenqp
  • 345
  • 4
  • 19

2 Answers2

1

Crashlog show that you don't register your cell with ViewController. Refer this to solve the problem : UICollectionView's cell registerClass in Swift

King.lbt
  • 843
  • 5
  • 15
0

could not dequeue a view of kind: UICollectionElementKindCell with identifier areaCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard

This clearly messages you what is the problem, you haven't registered the cell

You can do this by two ways

1) You can set the cell name "areaCell" in attribute inspector in storyboard 2) or in viewDidLoad, you can simply add

areaCView.registerClass(customCell.self, forCellWithReuseIdentifier: "areaCell")
Ankur Lahiry
  • 2,253
  • 1
  • 15
  • 25