我正在制作一个使用 Blogger API 的应用程序。在第一个选项卡中,我可以搜索帖子并显示其内容。另外,我可以将帖子添加到第二个选项卡中的“收藏夹”部分。一切正常,直到我关闭应用程序。重新启动后,收藏夹部分消失了。我尝试实现 UserDefaults,以便在终止应用程序后“收藏夹”部分不会变空,但它不起作用。
这是将帖子添加到收藏夹的按钮的代码:
vc.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addTapped))
func addTapped() {
offlineTitles.append(cellText)
titlesArray.append(cellText)
subtitlesArray.append(cellSubtitle)
let defaults = UserDefaults.standard
defaults.set(titlesArray, forKey: "title")
defaults.set(subtitlesArray, forKey: "subtitle")
NotificationCenter.default.post(name: .reload, object: nil)
let ac = UIAlertController(title: "Added!", message: "Post added to favorites", preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "Great!", style: .default))
present(ac, animated: true)
}
这是FavoritesViewController.swift:
import UIKit
var offlineTitles = [String]()
var titlesArray = [String]()
var subtitlesArray = [String]()
extension Notification.Name {
static let reload = Notification.Name("reload")
}
class OfflineViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(reloadTableData(_:)), name: .reload, object: nil)
self.tableView.allowsMultipleSelectionDuringEditing = false
}
func reloadTableData(_ notification: Notification) {
self.tableView.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return titlesArray.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "OfflineCell", for: indexPath)
let defaults = UserDefaults.standard
let userDefaultsTitleArray = defaults.array(forKey: "title") as? [String] ?? [String]()
let userDefaultsSubtitleArray = defaults.array(forKey: "subtitle") as? [String] ?? [String]()
let title = userDefaultsTitleArray[indexPath.row]
let subtitle = userDefaultsSubtitleArray[indexPath.row]
cell.textLabel?.text = title
cell.detailTextLabel?.text = subtitle
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let vc = FavouritesViewController()
navigationController?.pushViewController(vc, animated: true)
}
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == UITableViewCellEditingStyle.delete {
offlineTitles.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic)
}
}
}
最佳答案
您似乎正在读取 cellForRowAt
中的用户默认值。这不仅效率低下(如果你有五个最喜欢的,你会读五遍),而且时机不对。例如,numberOfRowsInSection
将返回什么?当调用时,您还没有将用户默认值读入数组。
您应该将用户默认值读入 viewDidLoad
中的数组(也可能在 reloadTableData
中)。
关于ios - 在 UITableView 上使用 UserDefaults,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41535818/