如何在Swift 3中保存UITableViewCell附件复选标记的状态

时间:2017-03-18 03:50:23

标签: swift uitableview tableviewcell accessoryview userdefaults

我有一个表格视图,我希望用户只从表格视图中选择一个项目,然后我想在单元格中添加一个复选标记。与此同时,我也希望其他单元格上的复选标记消失。用户一次只能选择一个。然后我想保存数据(最好使用UserDefaults)。我该怎么做?

2 个答案:

答案 0 :(得分:1)

创建IndexPath类型的类变量。在您的cellForRowAt方法中,仅当索引路径与类变量匹配时才设置.checkmark附件类型,否则为.none。在didSelectRowAt方法中,更新所选变量的indexPath并重新加载tableview

答案 1 :(得分:0)

你应该以这种方式使用

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
    let sgp : UITableViewCell = tableView.cellForRow(at: indexPath)!
    sgp.accessoryType = .checkmark
}
相关问题