我已经为这类问题找到了很多解决方案,但我仍然卡住了,不知道如何将我的按钮状态保存到NSUserDefaults中。
这是我的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
}
这是我自定义的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)
}
答案 0 :(得分:0)
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
答案 1 :(得分:0)
将标记添加为 RecordCell 类中的变量,如
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 类中的
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变量。