在 NSUserDefaults 中的 Table 视图单元格中保存收藏夹按钮的状态

Save state of favorite button in Table view Cell in NSUserDefaults

我已经为这类问题解决了很多问题,但我仍然卡住了,不知道如何将我的按钮状态保存到 NSUserDefaults 中。

Here is the picture for demo

这是我的 Tableview 控制器

override func viewDidLoad() {

    super.viewDidLoad()
    navigationItem.title = "Contacts"
    tableView.register(RecordCell.self, forCellReuseIdentifier: "cellId")
}


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let myCell = tableView.dequeueReusableCell(withIdentifier: "cellId", for: indexPath as IndexPath) as! RecordCell


    myCell.starButton.tag = indexPath.row
    myCell.myTableViewController = self
    return myCell
}

这是我的自定义单元格class

class RecordCell: UITableViewCell {

var myTableViewController: HomepageController?

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    setupViews()
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

let nameLabel: UILabel = {
    let label = UILabel()
    label.translatesAutoresizingMaskIntoConstraints = false
    label.font = UIFont (name: "SFCompactDisplay-Regular", size: 14)
    return label
}()
let companyLabel: UILabel = {
    let label = UILabel()
    label.translatesAutoresizingMaskIntoConstraints = false
    label.font = UIFont (name: "SFCompactDisplay-Light", size: 10)
    return label
}()
let shortname: UILabel = {
    let label = UILabel()
    label.textColor = UIColor.white
    label.translatesAutoresizingMaskIntoConstraints = false
    label.font = UIFont (name: "SFCompactDisplay-Regular", size: 12)
    label.textAlignment = .center
    label.backgroundColor = UIColor(patternImage: UIImage(named: "avatar")!)
    return label
}()
let starButton: UIButton = {
    let button = UIButton(type: .system)
    button.setImage(UIImage(named: "star"), for: .normal)
    button.translatesAutoresizingMaskIntoConstraints = false
    return button
}()
let playButton: UIButton = {
    let button = UIButton(type: .system)
    button.setImage(UIImage(named: "playback"), for: .normal)
    button.translatesAutoresizingMaskIntoConstraints = false
    return button
}()

func setupViews() {
    addSubview(nameLabel)
    addSubview(companyLabel)
    addSubview(shortname)
    addSubview(starButton)
    addSubview(playButton)

    starButton.addTarget(self, action: #selector(RecordCell.starAction), for: .touchUpInside)

// playButton.addTarget(self, action: #selector(RecordCell.playAudio), for: .touchUpInside)

    addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-8-[v4(40)]-8-[v0]-8-[v1(30)]-8-[v2(30)]-8-|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": nameLabel, "v1": starButton,"v2":playButton, "v4":shortname]))
    addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-56-[v3]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v3": companyLabel]))


    addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-2-[v0][v3]-7-|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": nameLabel,"v3": companyLabel]))
    addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": starButton]))
    addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": playButton]))
    addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-2-[v0(40)]-7-|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": shortname]))

}

func starAction() {
    starButton.setImage(UIImage(named:"star-active"), for: .normal)
}
func starAction() {

     let userDefault = UserDefault.standard
      userDefault.set(true, forKey: "KEY_IS_SELECTED")
    starButton.setImage(UIImage(named:"star-active"), for: .normal)
}

获取最喜欢的按钮状态:

let userDefault = UserDefault.standard
let state = userDefault.bool(forKey: "KEY_IS_SELECTED")

//get stored state of button
//use state of button

RecordCell 中添加标签作为变量 class like

class RecordCell: UITableViewCell {

var myTableViewController: HomepageController?
var favID:Int = 0
.....//other code
}

Viewcontroller

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let myCell = tableView.dequeueReusableCell(withIdentifier: "cellId", for: indexPath as IndexPath) as! RecordCell

    myCell.favID = indexPath.row //set value to favID variable
    myCell.myTableViewController = self
    myCell.updateSelection() //call this method to update the selection
    return myCell
}

RecordCell class

override func awakeFromNib() {
    super.awakeFromNib()
    updateSelection()
}

func updateSelection() {
    let key = "\(favID)" 
    let userDefault = UserDefault.standard
    let isFav = userDefault.bool(forKey: key)
    starButton.isSelected = isFav
}

let starButton: UIButton = {
    let button = UIButton(type: .system)
    button.setImage(UIImage(named: "star"), for: .normal) //add image for different states
    button.setImage(UIImage(named:"star-active"), for: .selected) //add image for different states
    button.translatesAutoresizingMaskIntoConstraints = false
    return button
}()

func setupViews() {
    ....
    ....//other code
    updateSelection()
}

func starAction() {
    starButton.isSelected = !starButton.isSelected //toggle selection
    let key = "\(favID)" 
    let userDefault = UserDefault.standard
    userDefault.set(starButton.isSelected, forKey: key)
    userDefault.synchronize()
}

这是一个基于索引的示例,如果可能,您可以使用 table 中显示的对象的任何唯一标识符作为键。或者在数据模型本身中设置一个 isFav 变量。