我正在尝试从数组中检索值并使用它们来填充表视图。
问题是我收到了这个错误:
NSUnknownKeyException',原因:'[< NSObject 0x7fb54b78e610> setValue:forUndefinedKey:]:此类不是键值bookTitle的键值编码兼容。
我的代码:
书类
class Book: NSObject {
var allBooks: [Book] = []
var bookAuthor: String
var bookTitle: String
init (bookAuthor: String, bookTitle: String) {
self.bookAuthor = bookAuthor
self.bookTitle = bookTitle
super.init()
}
}
BookStore Class
class BookStore: NSObject {
var allBooks: [Book] = []
func addBook(bookAuthor: String, bookTitle: String) {
let newBook = Book(bookAuthor: bookAuthor, bookTitle: bookTitle)
allBooks.append(newBook)
}
}
AddBookViewController
class AddBookViewController: UIViewController {
@IBOutlet weak var bookTitle: UITextField!
@IBOutlet weak var bookAuthor: UITextField!
let bookStore: BookStore
init (bookStore: BookStore) {
self.bookStore = bookStore
super.init(nibName: "AddBookViewController", bundle: nil)
navigationItem.title = "Add New Book"
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
@IBAction func importISBN(sender: AnyObject) {
}
@IBAction func saveNewBook(sender: AnyObject) {
let author = self.bookAuthor.text!
let title = self.bookTitle.text!
println("Author: \(author) Title: \(title)")
bookStore.addBook(author, bookTitle: title)
println("\(self.bookStore.allBooks.count)")
}
}
TableViewController
class BookListViewController: UITableViewController {
@IBAction func addNewBook (sender: UIButton) {
let addBookVC = AddBookViewController(bookStore: bookStore)
let navController = UINavigationController(rootViewController: addBookVC)
navigationController!.pushViewController(addBookVC, animated: true)
}
let bookStore: BookStore
init(bookStore: BookStore) {
self.bookStore = bookStore
super.init(nibName: nil, bundle: nil)
navigationItem.title = "Books"
let addBook = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "addNewBook:")
navigationItem.rightBarButtonItem = addBook
}
required init!(coder aDecoder: NSCoder!) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.rowHeight = 44
let nib = UINib(nibName: "ItemCell", bundle: nil)
tableView.registerNib(nib, forCellReuseIdentifier: "ItemCell")
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(true)
dispatch_async(dispatch_get_main_queue()) {
self.tableView.reloadData()
}
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return bookStore.allBooks.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("ItemCell", forIndexPath: indexPath) as! ItemCell
let books = bookStore.allBooks[indexPath.row]
cell.bookTitle.text = books.bookTitle
return cell
}
}
编辑:
答案 0 :(得分:1)
当您的界面构建器出口连接错误时,就会发生IT。尝试从UITableViewCell重新创建出口并删除所有错误的出口(标记为'!'在右侧面板)