0

swift5でUICollectionViewを使用し、Cellはxlbを使用したカスタマイズドセルにするつもりです。
このモジュールはSwiftを始めた頃に習って作ったのですが、新規のアプリを作るのでやり直してみたのですがエラーが発生してしまいました。
昔、このようなエラーを見た記憶があり「login_cLP」というkeyが場所により違っていたためでした。そのために全て手で入れ直したのですが、やはりだめでした。

エラーメッセージ

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier login_cLP - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
terminating with uncaught exception of type NSException

ソースコード

import UIKit
import StoreKit

class MenuViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

    @IBOutlet weak var loginIdView: UICollectionView!
    @IBOutlet weak var explanationView: UICollectionView!
    @IBOutlet weak var recordCalendarView: UICollectionView!
    @IBOutlet weak var bannerImage: UIImageView!
    

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

        // navigationItemにタイトル
        self.navigationItem.title = contract.office
        
        // 日付管理クラスのイニシャライズ
        cm.cdm_init()

        //カスタムセルをcollectionViewに登録する
        //カスタムセルを取ってくる
        let cLP = 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")
        
        let cLC = UINib(nibName: "Cell_Label_Command", bundle: nil)
        self.recordCalendarView.register(cLC, forCellWithReuseIdentifier: "calendar_cLC")
        
    以下省略
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
//        print("collectionView.tag",collectionView.tag,indexPath, product)
        switch collectionView.tag {
        case menuLoginTag:
            let cell = prepare_Cell_Label_Print(viewType: collectionView, indexPath: indexPath, key: "login_cLP", value: monthlyLoginLabel[indexPath.row], attribute: monthlyLoginIdAttribute[indexPath.row])
    以下省略
        case menuExplanationTag:
            let cell = prepare_Cell_Label_Print(viewType: collectionView, indexPath: indexPath, key: "explanation_cLP", value: explanationMenuLabel[indexPath.row], attribute: explanationMenuAttribute[indexPath.row])

    以下省略

        default:  // menuCalendarTag

    以下省略

        }
    }
}

import UIKit

class Cell_Label_Print: UICollectionViewCell {

    @IBOutlet weak var BackView: UIView!
    @IBOutlet weak var ForeView: UIView!
    @IBOutlet weak var contents: UILabel!
    
    @IBOutlet weak var top: NSLayoutConstraint!
    @IBOutlet weak var trailing: NSLayoutConstraint!
    @IBOutlet weak var leading: NSLayoutConstraint!
    @IBOutlet weak var bottom: NSLayoutConstraint!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func prepareForReuse() {
        super.prepareForReuse()

        contents.attributedText = nil
    }
}

1 件の回答 1

0

カスタムセルを表示するViewまたはViewControllerawakeFromNib()の中で、xib(nib)registerする必要があるのでは無いでしょうか?
カスタムセルを利用する際はawakeFromNib辺りの中で

guard let nib: NSNib = NSNib(nibNamed: "カスタムセルのxib名", bundle: Bundle.main) else { continue }
let identifier: NSUserInterfaceItemIdentifier = NSUserInterfaceItemIdentifier(rawValue: "カスタムセルのIdentifier")
self.tableViewComment.register(nib, forIdentifier: identifier)

などのようにして、このViewインスタンスはカスタムセルをこのIdentifierで使用するので登録してね。というのを行わないといけないと思います。

3
  • このアプリでは上記を以下でやっていると思います。 //カスタムセルをcollectionViewに登録する //カスタムセルを取ってくる let cLP = 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") 2022年2月11日 1:12
  • 追加ですが、実際にエラーが発生しているのは cellForItemAtで呼び出している以下の関数の1行目でした。 func prepare_Cell_Label_Print(viewType: UICollectionView, indexPath: IndexPath, key: String, value: String, attribute: (Char_Color: Int, Char_Style: Int, Char_Position: Int, BackgroundColor: Int, Type: Int, Func: Int)) -> Cell_Label_Print { let cell = viewType.dequeueReusableCell(withReuseIdentifier: key, for: indexPath) as! Cell_Label_Print 2022年2月11日 1:30
  • 同じnibviewによってidentifierを変えているのが気になります。identifierを統一してみたらどうなるでしょうか?あと、prepare_Cell_Label_Printがなにをしているのか記述がないのでその先は解りません。
    – Чайка
    2022年2月11日 7:02

この質問に回答するには、ログインする必要があります。

求めていた回答ではありませんか? のタグが付いた他の質問を参照する。