小村祐輝
小村祐輝

Reputation: 73

I can not solve the error "coding classification not key value coding-compliant for the key"

It is a question.

This class is not key value code-compliant for the key error is given.

However, I am not sure which part is wrong. This error appears at cellForRowAt.

Thank you for your answer.

If you want it, I can also show the code of ContentTableViewCell, so please call out.

import UIKit

class SearchViewController: UIViewController, UISearchResultsUpdating {

@IBOutlet weak var tableview: UITableView!

var songs = [
    "裁量権を持って、若者向けカレンダーアプリ開発をしたいiOSエンジニア募集!",
    "自信を持って、応援出来るエンジニアを募集!"
]

var searchSongs = [String]()

func updateSearchResults(for searchController: UISearchController) {
    let searchString = searchController.searchBar.text!
    searchSongs = songs.filter {(name) -> Bool in
        return name.contains(searchString)
    }
    tableview.reloadData()
}

override func viewDidLoad() {
    super.viewDidLoad()
    navigationController?.navigationBar.prefersLargeTitles = true
    let searchController = UISearchController(searchResultsController:nil)
    searchController.searchResultsUpdater = self
    searchController.dimsBackgroundDuringPresentation = false
    navigationItem.searchController = searchController
    definesPresentationContext = true
    delegate()
}
}




extension searchViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if navigationItem.searchController?.isActive == true {
        return searchSongs.count
    } else {
        return songs.count
    }
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableview.dequeueReusableCell(withIdentifier: "ContentTableViewCell", for: indexPath) as! ContentTableViewCell
    if navigationItem.searchController?.isActive == true {
        cell.commonInit(content: searchSongs[indexPath.row])
    } else {
        cell.commonInit(content: songs[indexPath.row])
    }
    return cell
}

func delegate() {
    tableview.delegate = self
    tableview.dataSource = self
    let nibName = UINib(nibName: "ContentTableViewCell", bundle: nil)
    self.tableview.register(nibName, forCellReuseIdentifier: "ContentTableViewCell")
}

}

Upvotes: 0

Views: 154

Answers (1)

nitin.agam
nitin.agam

Reputation: 2142

Check in your Storyboard. I think any outlet is not connected properly. enter image description here

Check is there any yellow warning triangle in your storyboard on which screen this error is coming. If you found any, remove and connect the outlet again.

Upvotes: 2

Related Questions