swift - 在 UITableView 行中保存按钮的选择状态(Swift)
问题描述
我有一个连续调用的btnChk2
按钮UITableview
。当用户按下btnChk2
按钮btnChk
被选中时。上面的代码可以实现这一点,但是当我退出应用程序并打开它时,复选框的状态是不一样的,例如,我想要的是如果选中了一个行复选框,我希望在用户离开时该应用程序和复出相同的复选框保持选中状态,尝试过NSUserDefaults
但没有与我合作。
这是有效的代码,但它不保存复选框的状态:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CheckBoxCell")
if let lbl = cell?.contentView.viewWithTag(1) as? UILabel {
lbl.text = "item-\(1)"
}
if let btnChk = cell?.contentView.viewWithTag(2) as? UIButton {
btnChk.addTarget(self, action: #selector(checkboxClicked(_ :)), for: .touchUpInside)
}
if let btnChk2 = cell?.contentView.viewWithTag(100) as? UIButton {
btnChk2.addTarget(self, action: #selector(checkboxClicked(_ :)), for: .touchUpInside)
}
return cell!
}
@objc func checkboxClicked(_ sender: UIButton) {
guard let cell = sender.superview?.superview as? UITableViewCell else {
return
}
if sender.tag == 2 {
if let btnChk2 = cell.contentView.viewWithTag(100) as? UIButton {
if btnChk2.isSelected == true {
btnChk2.isSelected = false
}else{
btnChk2.isSelected = true
}
}
sender.isSelected = false
}else if sender.tag == 100{
if let btnChk = cell.contentView.viewWithTag(2) as? UIButton {
if btnChk.isSelected == true {
btnChk.isSelected = false
}else{
btnChk.isSelected = true
}
}
}
}
解决方案
建议不要使用 userdefault,user default 仅用于用户设置目的,最好使用核心数据或保存在 plist 文件中。无论如何,这里是使用UserDefaults
.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell : UITableViewCell!
// code to check for a particular tableView to display the data .
if tableView == ScheduleTableView{
cell = tableView.dequeueReusableCell(withIdentifier: "ScheduleCell", for: indexPath as IndexPath)
let lblCity = cell.viewWithTag(1)as! UILabel
lblCity.text = ScheduleArray[indexPath.row] as String
if let btnChk2 = cell?.contentView.viewWithTag(100) as? UIButton {
btnChk2.addTarget(self, action: #selector(checkboxClicked(_ :)), for: .touchUpInside)
}
if let btnChk2 = cell.contentView.viewWithTag(22) as? UIButton {
btnChk2.isSelected = (UserDefaults.standard.integer(forKey: String(format: "indexpath%d", (indexPath.row))) == indexPath.row) ? true : false
}
}
if tableView == GoalsTableView{
cell = tableView.dequeueReusableCell(withIdentifier: "GoalsCell", for: indexPath as IndexPath)
let lbl3 = cell.viewWithTag(2)as! UILabel
lbl3.text = GoalsArray[indexPath.row]as? String
}
return cell
}
@objc func checkboxClicked(_ sender: UIButton) {
print("AAAA")
// sender.isSelected = !sender.isSelected
guard let cell = sender.superview?.superview as? UITableViewCell else {
return
}
let buttonPosition:CGPoint = sender.convert(CGPoint.zero, to:self.ScheduleTableView)
let indexPath = self.ScheduleTableView.indexPathForRow(at: buttonPosition)
if sender.tag == 100 {
if let btnChk2 = cell.contentView.viewWithTag(22) as? UIButton {
if btnChk2.isSelected == true {
btnChk2.isSelected = false
UserDefaults.standard.set(-1, forKey: String(format: "indexpath%d", (indexPath?.row)!))
}else{
btnChk2.isSelected = true
UserDefaults.standard.set(indexPath?.row, forKey: String(format: "indexpath%d", (indexPath?.row)!))
}
}
sender.isSelected = false
}
}
推荐阅读
- node.js - MongoDB 如何修复:错误:无法连接到服务器 197.0.196.205:27017,连接尝试失败:SocketException
- java - TestNG 智能感知在 Eclipse 霓虹灯中不起作用
- json - 从oracle中的Json Array字符串中选择所有列
- go - gorm 多对一返回空
- extjs - 添加新行或编辑现有行时行编辑插件的奇怪行为
- javascript - 只是想看看是否有一种方法可以将字符串变量连接到 onclick 函数上?
- r - 求和 n 列后满足条件的列名
- java - 如何在不使用 setZOrderOnTop(true) 函数的情况下使 SurfaceView 的背景透明?
- eloqua - Eloqua API - 根据安全标签检索联系人列表
- c# - paging crm 动态结果高于 5k “需要 PagingCookie”