gpt4 book ai didi

ios - 从数组中检索 TableView 单元格标签的值

转载 作者:行者123 更新时间:2023-11-29 01:54:45 25 4
gpt4 key购买 nike

我正在尝试从数组中检索值并使用它们来填充 TableView 。

问题是我遇到了这个错误:

NSUnknownKeyException', reason: '[<NSObject 0x7fb54b78e610> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key 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()
}

}

书店类

    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)")
}

}

表格 View Controller

    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
}
}

编辑:

enter image description here enter image description here enter image description here

最佳答案

当您的界面构建器的 socket 连接错误时,就会发生这种情况。尝试从您的 UITableViewCell 重新创建 socket 并删除所有错误的 socket (它们在右侧面板中标记为“!”)

enter image description here

关于ios - 从数组中检索 TableView 单元格标签的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31005786/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com