如何将选定的复选标记设置为在 tableview 单元格 swift 3.0 中打勾 [英] how to set the selected check mark as ticked in tableview cell swift 3.0

查看:22
本文介绍了如何将选定的复选标记设置为在 tableview 单元格 swift 3.0 中打勾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个 UITableView 来选择带有刻度线的国家.但是当我移动到另一个屏幕时,当我回来时,我的复选标记是不可见的.似乎我选择的国家很好,但是当我移动到其他屏幕并回来后,所选的勾号不在那里.如何快速做到这一点.

I am using one UITableView to select the country with tick mark. But when I move to other screen and when I come back my check mark is invisible. It seems like the country what I am selecting is fine, But after I move to other screen an come back, The selected tick mark is not there. How to do that in swift.

我的代码:

    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var saveBtn: UIButton!
    var languageName : String = String()

    var option : [String] = ["English","हिंदी"]
    var option1 : [String] = []
    let availableLanguages = Localize.availableLanguages()

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.tableFooterView = UIView()
        for language in availableLanguages {
            option1.append(language)
            let displayName = Localize.displayNameForLanguage(language)

        }

    }




    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

    }



    //MARK: - TableView
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return option1.count
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 60
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

        cell.textLabel?.text = option[indexPath.row]

        if option1[indexPath.row] == languageName{
            cell.accessoryType = UITableViewCellAccessoryType.checkmark
        }else{
            cell.accessoryType = UITableViewCellAccessoryType.none
        }
        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        languageName = option1[indexPath.row]
        self.tableView.reloadData()
    }


    @IBAction func saveButtonPressed(_ sender: Any) {
        Localize.setCurrentLanguage(languageName)
        if let appdelegate = UIApplication.shared.delegate as? AppDelegate {
            appdelegate.showHomeLandingScreen()
        }
    }

推荐答案

1) 创建另一个选定项目的数组并保存它有很多选项,例如.UserDefaults.standard

1) create another array of selected items and save it there are so many options eg. UserDefaults.standard

2) 然后与 option1[indexPath.row]

示例

UserDefaults.standard.set(selectedLanguageArray, forKey: "selectedLanguageArray")
UserDefaults.standard.synchronize()

然后得到它

UserDefaults.standard.value(forKey: "selectedLanguageArray")

这篇关于如何将选定的复选标记设置为在 tableview 单元格 swift 3.0 中打勾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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