gpt4 book ai didi

ios - 将表格 View 单元格中收藏夹按钮的状态保存在 NSUserDefaults 中

转载 作者:行者123 更新时间:2023-11-30 12:15:20 26 4
gpt4 key购买 nike

我已经针对此类问题尝试了很多解决方案,但我仍然陷入困境,并且不知道如何将按钮的状态保存到 NSUserDefaults 中。

Here is the picture for demo

这是我的 Tableview Controller

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
}

这是我的自定义 Cell 类

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)
}

最佳答案

RecordCell类中添加标签作为变量

class RecordCell: UITableViewCell {

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

View Controller

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类中

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()
}

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

关于ios - 将表格 View 单元格中收藏夹按钮的状态保存在 NSUserDefaults 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45498300/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com