如何将复选框的状态保存到Swift中的核心数据? [英] How to save the state of the checkbox to core data in Swift?

查看:66
本文介绍了如何将复选框的状态保存到Swift中的核心数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

**我的英语说得不好。请原谅我的英语不好。

**I'm not a good English speaker. Please forgive me for my awkward English.

我正在使用复选框进行待办事项列表项目。我找不到将复选框状态保存到核心数据的方法

I'm working on a to do list project using checkbox. I cannot find a way to save the state of the checkbox to core data

这是我现在使用的代码的一部分。 任务是实体(类定义),并且具有 isMarked 作为布尔属性。
(为了简化起见,我做了很多工作,因此,如果您在代码中发现了奇怪的地方,请写评论)

This is a part of the code I use right now. Tasksis the Entity (class definition) and it has isMarked as a Boolean Attribute. (I cut a lot to make it simple so if you find something strange in the code please write a comment)

import UIKit
import CoreData

var toDolist: [Tasks] = []

class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{


    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.addSubview(toDoTable)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    @IBOutlet weak var toDoTable: UITableView!

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        return toDolist.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! CustomTableViewCell
        cell.box.setImage(#imageLiteral(resourceName: "uncheckedbox"), for: .normal)

        let task = toDolist[indexPath.row]

        return cell
    }

    func getData(){
        let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
        do{
            toDolist = try context.fetch(Tasks.fetchRequest())
        }catch{
            print("fetching failed")
        }
    }

    override func viewWillAppear(_ animated: Bool) {
        getData()
        toDoTable.reloadData()
    }

}


class CustomTableViewCell: UITableViewCell {
    override func awakeFromNib() {
        super.awakeFromNib()
    }
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }
    @IBOutlet weak var box: CheckBox!
    @IBOutlet weak var taskLbl: UILabel!
}

class CheckBox{
    var isChecked: Bool = false{
        didSet{
            if isChecked == true{
                self.setImage(#imageLiteral(resourceName: "checkedbox"), for: .normal)
            }else{
                self.setImage(#imageLiteral(resourceName: "uncheckedbox"), for: .normal)
            }
        }
    }

    override func awakeFromNib(){
        self.addTarget(self, action: #selector(self.buttonClicked(_:)), for: .touchUpInside)
        self.isChecked = false
    }

    func buttonClicked(_ sender: UIButton){
        if sender == self{
            if isChecked == true{
                isChecked = false
            }else{
                isChecked = true
            }
        }
    }
}

如何通过添加一些代码来解决这个问题?还是我必须更改上面的所有代码?

How can I solve this by adding some code to it? Or do I have to change all the code above?

推荐答案

简单解决方案:


  • 删除 UIButton 的子类并将按钮的类还原为 UIButton

  • 在Interface Builder中,将图像分配为 checkedbox uncheckedbox 按钮以选择状态 默认。根据按钮的 isSelected 属性自动显示图像。这样可以避免完全更新代码中的图像。

  • Remove the subclass of UIButton and revert the class of the button to UIButton.
  • In Interface Builder assign the images checkedbox and uncheckedbox to the button for states selected and default. The images are displayed automatically depending on the isSelected property of the button. That avoids completely to update the images in code.

CustomTableViewCell 获得属性任务 IBAction 必须与 Touch Up Inside 按钮。 任务的属性 isSelected 必须更改为 NSManagedObject的属性名称

The class CustomTableViewCell got the property task and the IBAction which must be connected to Touch Up Inside of the button. The property isSelected of the task must be changed to the name of the attribute of the NSManagedObject

class CustomTableViewCell: UITableViewCell {

    @IBOutlet weak var box: UIButton!
    @IBOutlet weak var taskLbl: UILabel!

    var task : Tasks! {
        didSet {
            box.isSelected = task.isSelected
            taskLbl.text = // update the label
        }
    }

    @IBAction func buttonClicked(_ sender: UIButton)
    {
        let selected = !sender.isSelected
        sender.isSelected = selected
        task.isSelected = selected
        // save the context if needed
    }

}


  • cellForRowAt 中,添加一行以分配 task

  • In cellForRowAt just add the line to assign the task

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! CustomTableViewCell
        let task = toDolist[indexPath.row]
        cell.task = task
        return cell
    }
    


  • 这篇关于如何将复选框的状态保存到Swift中的核心数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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