如何将Checkmark保存在Swift中



我正在为自己制作一个todo应用程序,我想制作一个函数以将todo标记为完成。它添加了一个选中标记,并且字体应变为灰色,但是我是编码的新手,因此我真的不知道如何保存字体颜色和存储符号为内存。我不知道我是否应该将其保存到UserDefaults或Core Data,最重要的是如何保存它。非常感谢任何帮助。

这是代码:我想保存textcolor和accoresorytype

func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    let done = UIContextualAction(style: .normal, title: "Done") { (action, view, nil) in
        print("Done")
        tableView.cellForRow(at: indexPath)?.textLabel?.textColor = UIColor.lightGray
       tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
    }
    done.backgroundColor = .blue
    let config = UISwipeActionsConfiguration(actions: [done])
    config.performsFirstActionWithFullSwipe = false
    return config           
}

在您的数据模型中添加属性

var isDone : Bool = false

cellForRow中,根据该属性设置UI(假设datasourceArray是数据源数组)

let item = datasourceArray[indexPath.row]
cell.textColor = item.isDone ? .lightGray : .blue
cell.accessoryType = item.isDone ? .checkmark : .none

在操作中,将isDone属性设置为true并重新加载行。

let done = UIContextualAction(style: .normal, title: "Done") { (action, view, nil) in
    print("Done")
    self.datasourceArray[indexPath.row].isDone = true
    self.tableView.reloadRows(at: [indexPath], with: .none)
}

和删除<罢工>

done.backgroundColor = .blue

您可以添加带有var状态的todo:bool?将其存储在数据库上。您可以通过设置(状态)设置带有状态的选中标记希望帮你!

最新更新