【发布时间】:2020-07-31 16:04:01
【问题描述】:
我有一个应用程序,用户可以通过填充表格视图将书籍添加到收藏夹列表中。这本书的详细信息显示在一个视图中,通过点击“收藏夹”,执行一个搜索,将该书的信息输入到一个表格视图单元格中。
目前一次只能在表格中出现一本书,添加新书将删除初始条目(因此实际上只使用了 tableview 的第一个单元格)
有没有办法将每个条目保存在 tableview 中,所以实际上创建了收藏夹列表
保存按钮
@IBAction func saveButton(_ sender: Any) {
let bookFormat = formatLabel.text
if (bookFormat!.isEmpty)
{
displayMyAlertMessage(userMessage: "Please Select a Book Format")
return
}
else{
self.performSegue(withIdentifier: "LibViewSegue", sender: self)
}
}
表格视图
extension LibrarybookViewController: UITableViewDataSource, UITableViewDelegate{
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 115
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print(#function, dataSource.count)
return dataSource.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
print(#function, "indexPath", indexPath)
guard let bookCell = tableView.dequeueReusableCell(withIdentifier: "libCell", for: indexPath) as? LibrarybookTableViewCell else {
return UITableViewCell()
}
let libbook = dataSource[indexPath.row]
bookCell.cellTitleLabel.text = libbook.title
bookCell.cellReleaseLabel.text = libbook.release
bookCell.cellFormatLabel.text = bookFormat
return bookCell
}
我一直在阅读有关默认值和 CoreData 的信息,但我不确定这应该在 segue 按钮操作中还是在 tableview 函数中实现?
【问题讨论】:
标签: swift uitableview core-data datapersistance