我在UITableView控制器中实现了一个复选框,它工作正常,但是当选中该复选框并删除tableview中的行时,当我向tableview添加新行时,它会被选中。 这是我项目的链接:https://files.fm/f/tepkz2du 我认为它与删除行时有关,索引路径搞砸了。
更新后的代码:
cellForRowAt:
if let btnChk3 = cell?.contentView.viewWithTag(100) as? UIButton {
btnChk3.addTarget(self, action: #selector(checkboxClicked(_ :)), for: .touchUpInside)
}
if let btnChk2 = cell.contentView.viewWithTag(22) as? UIButton {
btnChk2.isSelected = UserDefaults.standard.integer(forKey: myarray[indexPath.row]) == (indexPath.row + index) ? true : false
}
checkboxClicked
let defaults = UserDefaults.standard
let myarray = defaults.stringArray(forKey: "ScheduleArray") ?? [String]()
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: myarray[(indexPath?.row)!] )
UserDefaults.standard.synchronize()
}else{
btnChk2.isSelected = true
UserDefaults.standard.set((indexPath?.row)! + index, forKey: myarray[(indexPath?.row)!])
UserDefaults.standard.synchronize()
}
}
sender.isSelected = false
}
editingStyle == .delete:
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
if tableView == ScheduleTableView{
let defaults = UserDefaults.standard
var myarray = defaults.stringArray(forKey: "ScheduleArray") ?? [String]()
print(myarray)
myarray.remove(at: indexPath.row)
defaults.set(myarray, forKey: "ScheduleArray")
ScheduleTableView.deleteRows(at: [indexPath], with: .fade)
}
}
答案 0 :(得分:2)
if let btnChk2 = cell.contentView.viewWithTag(22) as? UIButton {
btnChk2.isSelected = UserDefaults.standard.integer(forKey: myarray[indexPath.row]) == (indexPath.row + index) ? true : false
}
更改cellForRowAt
let defaults = UserDefaults.standard
let myarray = defaults.stringArray(forKey: "ScheduleArray") ?? [String]()
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: myarray[(indexPath?.row)!] )
UserDefaults.standard.synchronize()
}else{
btnChk2.isSelected = true
UserDefaults.standard.set((indexPath?.row)! + index, forKey: myarray[(indexPath?.row)!])
UserDefaults.standard.synchronize()
}
}
sender.isSelected = false
}
CHANGE IN checkboxClicked
答案 1 :(得分:1)
不要在UserDefaults中保存索引路径,保存您在表格单元格中显示的任何值。并将其与cellForRowAtIndex中的数组匹配。像我一样
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let c = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! tabCell
c.lblC.text = arry?[indexPath.row]
/* for example
arry = ["First","Second","Third","Fourth","Fifth","Sixth","Seventh","eight","Ninth","Tenth"]
seleted = ["First","Fifth"]
*/
print("indexpath--",indexPath.row)
if selected != nil
{
if (selected?.contains((arry?[indexPath.row])!))!
{
c.btnCheck.setBackgroundImage(#imageLiteral(resourceName: "check.png"), for: .normal)
}
else
{
c.btnCheck.setBackgroundImage(#imageLiteral(resourceName: "uncheck.png"), for: .normal)
}
}
c.btnCheck.tag = indexPath.row
c.btnCheck.addTarget(self, action:#selector(btnCheckClick(btn:)) , for: .touchUpInside)
return c
}
我无法运行您的代码...... 这就是我制作演示的方式 检查一下,希望你能有所了解...... https://files.fm/u/hj7jz9dj
答案 2 :(得分:0)
您可以在UITableViewCell中使用func prepareForReuse()
方法,以确保在插入新行时取消选中该复选框。
答案 3 :(得分:0)
您可以根据其索引路径在userdefaults中存储已选中按钮的值。这种态度的问题是当你删除单元格时,新单元格正在获取前一个单元格的索引路径错误。
您应该使用模型来表示您的行。存储在" ScheduleArray"一个对象,而不仅仅是一个字符串。
类似的东西:
{{1}}