在 UITableView 行中保存按钮的选择状态(Swift) [英] Save the select state of a button in UITableView row (Swift)

查看:76
本文介绍了在 UITableView 行中保存按钮的选择状态(Swift)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 UITableview 行上有一个名为 btnChk2 的按钮.当用户按下 btnChk2 按钮时 btnChk 被选中.上面的代码可以实现这一点,但是当我退出应用程序并重新打开它时,复选框的状态是不一样的,例如,我想要的是,如果选择了一行复选框,我希望用户离开时应用程序并返回相同的复选框保持选中状态,使用 NSUserDefaults 尝试过,但对我不起作用.

I have a button called btnChk2 on a UITableview row. When the user press the btnChk2 that the button btnChk get selected. The code above works to make that happen but when I quit the app and open it back up the status of the checkbox is the not the same, what I want for example is that if a row checkbox is selected, I want when the user leaves the app and comeback the same checkbox stay selected, tried it with NSUserDefaults but didn't worked with me.

这是有效的代码,但它不保存复选框的状态:

Here is the code that works but it doesn't save the state of the checkbox:

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 的解决方案.

Good suggestion don't use userdefault for this purpose, user default only use for user setting purpose, It's best to use core data or save in plist file. anyway here is solution using 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
    }


}

这篇关于在 UITableView 行中保存按钮的选择状态(Swift)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆