【发布时间】:2017-08-04 04:30:54
【问题描述】:
我已经为这类问题经历了很多解决方案,但我仍然卡住了,不知道如何将按钮的状态保存到 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)
}
【问题讨论】:
-
一个建议。将按钮的状态存储在用户默认值中并不是一个好的编程习惯。如果您真的想(重新)存储按钮的状态,更好的方法是创建一个单例类并将按钮的当前状态写入其中。
-
我是 Swift 新手,对单例模式一无所知。我只是尝试一些简单的东西。顺便说一句,非常感谢你的建议。我稍后会考虑
标签: ios swift uitableview uibutton nsuserdefaults