【问题标题】:How can I save data inside a TableView?如何在 TableView 中保存数据?
【发布时间】:2018-11-02 12:36:51
【问题描述】:

在我的应用程序中,我有一个标签,当用户点击视图或摇动 iPhone 时,它​​会随机更改引用。如果用户双击同一个视图,它应该将引用保存在 TableView 中。 一开始,我以为我可以用CoreData,但他没有用。现在我正在使用 UserDefaults,现在如果我双击视图,则会保存报价,但一次只能保存一个。我想要做的是他创建一个用户双击的所有引号的列表。

这是 doubleTap 对象内的代码:

let savedQuotes = UserDefaults.standard.setValue(quoteLabel.text!, forKey: "saveQuotes")

    if let printSavedQuotes = UserDefaults.standard.string(forKey: "saveQuotes"){

        print(printSavedQuotes)

    }

这是我在 TableVIew 内部使用的代码:

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    
    _ = UserDefaults.standard.string(forKey: "saveQuotes")

    return 15
    
}


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "QuoteCell", for: indexPath)
    
    if let printSavedQuotes = UserDefaults.standard.string(forKey: "saveQuotes"){
        
        cell.textLabel?.text = "\(printSavedQuotes)"
        
    }

这是问题的图片。

【问题讨论】:

  • 您不会将数据保存在 tableView 中。您使用它显示数据。另外,为什么你在 heightForRowAt 中有一个你甚至没有使用的 userdefaults 调用?

标签: ios swift uitableview core-data nsuserdefaults


【解决方案1】:

请了解 Swift 中的 Collections。你要找的是Array 类型。表示按特定顺序存储的元素集合的类型。

文档: https://developer.apple.com/documentation/swift/array

现在,当您学习如何将内容保存到数组中时,您可以将此数组连接到您的tableView

最基本的设置是:

var quotes: [String] = ["quote1", "quote2", "quote3"]

numberOfRowsInSection 你返回quotes.count

cellForRow 你的cell.textLabel.text == quotes[indexPath.row]

【讨论】:

    【解决方案2】:

    尝试如下方法..

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    
        let savedQuotes = UserDefaults.standard.value(forKey: "saveQuotes") as? [String] ?? [String]()
    
        return saveQuotes.count
    
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "QuoteCell", for: indexPath)
    
        let saveQuotes = UserDefaults.standard.value(forKey: "saveQuotes") as? [String] ?? [String]()
    
        cell.textLabel?.text = saveQuotes[indexPath.row]
    
        return cell
    }
    
    func saveQuote(){
    
        var saveQuotes = UserDefaults.standard.value(forKey: "saveQuotes") as? [String] ?? [String]()
    
        saveQuotes.append(quoteLabel.text!)
    
        UserDefaults.standard.set(saveQuotes, forKey: "saveQuotes")
    
        print(saveQuotes)
    }
    

    这只是一个例子,推荐的方法是使用 sqlite 或 core-data 来存储持久数据而不是 UserDefaults。

    【讨论】:

    • 不建议value(forKeyUserDefaults 检索数据。在这种情况下有array(forKey,在其他object 情况下使用object(forKey。无论如何都不鼓励使用UserDefaults 作为数据源。
    • 完美运行,谢谢。现在唯一的问题是如何删除保存的引号,因为现在它删除了我保存的所有引号
    猜你喜欢
    • 2013-03-16
    • 1970-01-01
    • 1970-01-01
    • 2018-09-07
    • 1970-01-01
    • 2013-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多