I am creating customized cells on Swift5 but I get an error.

Asked 1 years ago, Updated 1 years ago, 71 views

I'm going to use UICollectionView on swift5 and I'm going to use xlb for customized cells.
I learned this module when I started Swift and made it, but I tried again to create a new app, but an error occurred.
I remember seeing this kind of error a long time ago, and the key login_cLP was different depending on the location.So I put everything back in by hand, but it still didn't work.

error message

***Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not request a view ofkind:UICollectionElementKindCell with identifier login_cLP-must register a class for the identifier connection' probe into a cell
terminating with uncaught exception of type NSException

source code

import UIKit
import StoreKit

classMenuViewController:UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

    @IBOutletweak var loginIdView:UICollectionView!
    @IBOutlet weak var expansionView—UICollectionView!
    @IBOutlet weak var recordCalendarView: UICollectionView!
    @IBOutletweak varbannerImage:UIImageView!
    

    override func viewDidLoad(){
        super.viewDidLoad()
        // Do any additional setup after loading the view.

        // Title to navigationItem
        self.navigationItem.title=contract.office
        
        // Initializing a Date Management Class
        cm.cdm_init()

        // Register custom cells in collectionView
        // Get custom cells
        letcLP=UINib(nibName: "Cell_Label_Print", bundle:nil)
        self.loginIdView.register(cLP, forCellWithReuseIdentifier: "login_cLP")
        self.explanationView.register(cLP, forCellWithReuseIdentifier: "explanation_cLP")
        self.recordCalendarView.register(cLP, forCellWithReuseIdentifier: "calendar_cLP")
        
        letcLC=UINib(nibName: "Cell_Label_Command", bundle:nil)
        self.recordCalendarView.register(cLC, forCellWithReuseIdentifier: "calendar_cLC")
        
    hereinafter abbreviated
    }

    functioncollectionView(_collectionView:UICollectionView, cellForItemAtindexPath:IndexPath)->UICollectionViewCell{
//        print("collectionView.tag",collectionView.tag,indexPath,product)
        switch collectionView.tag {
        case menuLoginTag:
            letcell=prepare_Cell_Label_Print(viewType:collectionView,indexPath:indexPath,key:"login_cLP",value:monthlyLoginLabel [indexPath.row],attribute:monthlyLoginIdAttribute [indexPath.row]
    hereinafter abbreviated
        case menuExplanationTag:
            letcell=prepare_Cell_Label_Print(viewType:collectionView,indexPath:indexPath,key:"explanation_cLP",value:explanationMenuLabel [indexPath.row],attribute:explanationMenuAttribute [indexPath.row])

    hereinafter abbreviated

        default://menuCalendarTag

    hereinafter abbreviated

        }
    }
}

import UIKit

class Cell_Label_Print:UICollectionViewCell{

    @IBOutlet weak var BackView: UIView!
    @IBOutlet weak var ForeView: UIView!
    @IBOutlet weak var contents:UILabel!
    
    @IBOutlet peak:NSLayoutConstraint!
    @IBOutlet weak var training:NSLayoutConstraint!
    @IBOutlet weak var leading —NSLayoutConstraint!
    @IBOutlet weak varbottom:NSLayoutConstraint!
    
    override funcawakeFromNib(){
        super.awakeFromNib()
        // initialization code
    }

    override func prepareForReuse(){
        super.prepareForReuse()

        contents.attributedText=nil
    }
}

swift swift5 uicollectionview xib

2022-09-30 19:29

1 Answers

Does xib(nib) need to be register in View or ViewController where xib(nib) displays custom cells?
awakeFromNib when using custom cells

guardlet nib:NSNib=NSNib(nibNamed: "Custom Cell xib Name", bundle: Bundle.main) else {continue}
let identifier: NSUserInterfaceItemIdentifier = NSUserInterfaceItemIdentifier (rawValue: "Custom Cell Identifier")
self.tableViewComment.register(nib, forIdentifier:identifier)

In this way, this View instance uses custom cells in this Identifier, so please register.


2022-09-30 19:29

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.