質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.47%
Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

1391閲覧

'Could not load NIB in bundle: 'NSBundle<----.app>(loaded)' with name 'memoCell'

Kaguya_4869

総合スコア116

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2020/03/04 23:32

#質問したいこと
いつもお世話になっております。
現在、Todoリストのようなアプリを作っており、そこで以下のエラーが出てきてしまいます。

reason: 'Could not load NIB in bundle: 'NSBundle <.app> (loaded)' with name 'memoCell''

#コード

//一番最初の画面 import UIKit final class ViewController: UIViewController { // MARK: IBOutlet @IBOutlet weak var tableView: UITableView! // MARK: Properties private let model = UserDefaultsModel() private var dataSource: [Memo] = [Memo]() { didSet { tableView.reloadData() } } // MARK: Lifecycle static func instance() -> ViewController { let vc = UIStoryboard(name: "ViewController", bundle: nil).instantiateInitialViewController() as! ViewController return vc } override func viewDidLoad() { super.viewDidLoad() configureUI() configureTableView() } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) loadMemos() } // MARK: IBAction @objc private func onTapAddButton(_ sender: UIBarButtonItem) { let vc = AddViewController.instance() navigationController?.pushViewController(vc, animated: true) } } // MARK: - Configure extension ViewController { private func configureUI() { navigationItem.title = "メモ一覧" navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(onTapAddButton(_:))) } private func configureTableView() { tableView.delegate = self tableView.dataSource = self // print("tableView.delegate = self") OK tableView.tableFooterView = UIView() // print("tableView.FooterView = UIView()") OK tableView.rowHeight = TableViewCell.rowHeight // print("tableView.rowHeight") OK tableView.register(TableViewCell.nib, forCellReuseIdentifier: TableViewCell.reuseIdentifier) // print("tableview.regidter(TableViewCell.nib, forCellReuseIdentifier: TableViewCell.reuseIdentifier)") OK } } // MARK: - Model extension ViewController { private func loadMemos() { guard let memos = model.loadMemos() else { return } self.dataSource = memos // print("loadMemos") OK } } // MARK: - TableView dataSource, delegate extension ViewController: UITableViewDataSource, UITableViewDelegate { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { // print(dataSource.count) 011 return dataSource.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { tableView.register(TableViewCell.self, forCellReuseIdentifier: NSStringFromClass(TableViewCell.self)) // print("tableView.register(TableViewCell.self, forCellReuseIdentifier: NSStringFromClass(TableViewCell.self))") OK let cell = tableView.dequeueReusableCell(withIdentifier: TableViewCell.reuseIdentifier, for: indexPath) as! TableViewCell // print("let cell = tableView.dequeueReusableCell(withIdentifier: TableViewCell.reuseIdentifier, for: indexPath) as! TableViewCell") No let memo = dataSource[indexPath.row] cell.setupCell(title: memo.title, content: memo.content) return cell } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { tableView.deselectRow(at: indexPath, animated: true) let memo = dataSource[indexPath.row] let vc = DetailViewController.instance(memo) // セルの選択を解除 tableView.deselectRow(at: indexPath, animated: true) // 別の画面に遷移 performSegue(withIdentifier: "toDetail", sender: nil) navigationController?.pushViewController(vc, animated: true) } }
//カスタムセル import UIKit class TableViewCell: UITableViewCell { // MARK: IBOutlet @IBOutlet private weak var titleLabel: UILabel! @IBOutlet private weak var contentLabel: UILabel! // MARK: Static properties static let reuseIdentifier = "memoCell" static let rowHeight: CGFloat = 60 static var nib: UINib { return UINib(nibName: "memoCell", bundle: nil) } // MARK: Overrides override func awakeFromNib() { super.awakeFromNib() } override func setSelected(_ selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) } // MARK: Setup func setupCell(title: String, content: String) { titleLabel.text = title contentLabel.text = content } }

#やってみたこと
print()で調べてみたところ、ViewControllerの

let cell = tableView.dequeueReusableCell(withIdentifier: TableViewCell.reuseIdentifier, for: indexPath) as! TableViewCell

の部分でエラーが起きていると判明しました。
しかしながら、どのようにエラーを直せばいいのかわかりません。
#エラー

reason: 'Could not load NIB in bundle: 'NSBundle <.app> (loaded)' with name 'memoCell''

よろしくお願いいたします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

reason: 'Could not load NIB in bundle: 'NSBundle <.app> (loaded)' with name 'memoCell''

こちらのエラーはUINibをインスタンス化する時に
momoCellという名前のファイルを読みこむことができなかったため発生しています。

問題となっているのは以下の部分です。

Swift

1class TableViewCell: UITableViewCell { 2 3 // MARK: Static properties 4 5 static let reuseIdentifier = "memoCell" 6 static let rowHeight: CGFloat = 60 7 8 // この部分 9 static var nib: UINib { 10 return UINib(nibName: "memoCell", bundle: nil) 11 } 12}

クラス名がTableViewCellとなっているので、
xibファイルもTableViewCell.xibみたいな名前で作られていると思うので以下のようにすればセルをTableViewにきちんと登録できるはずです。

Swift

1class TableViewCell: UITableViewCell { 2 3 // MARK: Static properties 4 5 // ついでに reuse identifier も変えた方がいいと思います. 6 static let reuseIdentifier = "TableViewCell" 7 static let rowHeight: CGFloat = 60 8 9 // 正しいファイルを読み込むように引数nibNameに渡す文字列を変更. 10 static var nib: UINib { 11 return UINib(nibName: "TableViewCell", bundle: nil) 12 } 13}

もしxibファイルの名前が違うなら、その名前で上記の部分を書き換えてあげれば問題ないはずです。

投稿2020/03/05 05:55

hayabusabusash

総合スコア767

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Kaguya_4869

2020/03/05 10:49

回答ありがとうございます。 直りました! しかし、新しいエラーが発生してしまったので、新しく質問を建てさせていただくので、 そちらの方もよろしければ回答よろしくお願いします。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.47%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問