Post

Replies

Boosts

Views

Activity

Comment on How to add UserDefaults to save a table view button status?
First, thank you very much for your time. Second, where did you learn all of this? I'd love to have your level of understanding. I'm sorry for so many issues but now I get this: Thread 1: "Attempt to insert non-property list object (\n  "Go_Green_App.QuotesMainViewController.Quote(label: \"Quote1\", like: true)",\n  "Go_Green_App.QuotesMainViewController.Quote(label: \"Quote2\", like: false)",\n  "Go_Green_App.QuotesMainViewController.Quote(label: \"Quote3\", like: false)"\n) for key QuoteListKey"
Topic: UI Frameworks SubTopic: UIKit Tags:
Mar ’22
Comment on How to add UserDefaults to save a table view button status?
I cannot tell you how much I appreciate your help. I'm actually getting the error: Cannot assign value of type 'String' to type '[QuotesMainViewController.Quote]' I put the code below. Is there a better way to post it here? Maybe as an answer? import UIKit class QuotesMainViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {       @IBOutlet weak var quotesTableView: UITableView!       struct Quote {    var label: String    var like: Bool = false // a priori, false   }       var quoteList: [Quote] = []           override func viewDidLoad() {     super.viewDidLoad()           quotesTableView.delegate = self     quotesTableView.dataSource = self           let defaults = UserDefaults.standard     quoteList = UserDefaults.standard.string(forKey: "QuoteListKey") ?? [Quote(label: "Quote1"), Quote(label: "Quote2"), Quote(label: "Quote3")]         }       @IBAction func likeTapped(_ sender: UIButton) {          quoteList[sender.tag].like.toggle() // update the dataSource ; sender.tag gives the row in the array     if quoteList[sender.tag].like {      sender.setImage(UIImage(named: "GreenHeart"), for: .normal) // You can change here or ask for a reloadData()     } else {      sender.setImage(UIImage(named: "blankHeart"), for: .normal)     }     let defaults = UserDefaults.standard     defaults.set(quoteList, forKey: "QuoteListKey")  // or defaults.set(quoteLikes, forKey: "LikeKey")         }           func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {     return quoteList.count   }       func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {    let cell = quotesTableView.dequeueReusableCell(withIdentifier: "cell") as! QuotesTableViewCell          cell.quoteLabel.text = quoteList[indexPath.row].label    cell.likeButton.tag = indexPath.row  // Use tag to reference the cell, not to set true / false     cell.likeButton.setImage(UIImage(named: quoteList[indexPath.row].like ? "GreenHeart" : "blankHeart"), for: .normal)    return cell   }        } import UIKit class QuotesTableViewCell: UITableViewCell {   @IBOutlet weak var likeButton: UIButton!   @IBOutlet weak var quoteLabel: UILabel!       override func awakeFromNib() {     super.awakeFromNib()     // Initialization code   }   override func setSelected(_ selected: Bool, animated: Bool) {     super.setSelected(selected, animated: animated)     // Configure the view for the selected state   } }
Topic: UI Frameworks SubTopic: UIKit Tags:
Mar ’22