如何在tableview swift ios中显示保存的检查表
我希望我的如何在tableview swift ios中显示保存的检查表,ios,uitableview,checkbox,swift3,tableview,Ios,Uitableview,Checkbox,Swift3,Tableview,我希望我的tableView显示保存的数据。我试过了,但失败了。 保存并重新打开视图后,我希望保留清单 请帮我解决我的问题 这是我的代码: var id = [17,18,19] func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cellTheme: ThemeCell = self.tableView.dequeueRe
tableView
显示保存的数据。我试过了,但失败了。
保存并重新打开视图后,我希望保留清单
请帮我解决我的问题
这是我的代码:
var id = [17,18,19]
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellTheme: ThemeCell = self.tableView.dequeueReusableCell(withIdentifier: "cellTheme", for: indexPath) as! ThemeCell
cellTheme.themeLabel.text = listMenu[indexPath.row].theme
cellTheme.delegate = self
if id.isEmpty{
cellTheme.checkOutlet.image = UIImage(named: "ic_btn_home_checklist_off")
print("EMPTY")
}else{
for data in id{
print("TEST THEME \(id)")
print("DATA ! \(data)")
if listMenu[indexPath.row].id == data{
print("\(listMenu[indexPath.row].id) SAMA DENGAN DATA ")
cellTheme.checkOutlet.image = UIImage(named: "ic_btn_home_checklist_on")
}else{
print("\(listMenu[indexPath.row].id) TIDAK SAMA DENGAN DATA ")
cellTheme.checkOutlet.image = UIImage(named: "ic_btn_home_checklist_off")
}
}
}
return cellTheme
}
注意:列表菜单[indexath.row].id=[17,18,19,146]UserDefaults永久存储用户的数据。此数据将本地存储在设备上
// UserDefaults.standard().set("Rob", forKey: "name")
let nameObject = UserDefaults.standard.object(forKey: "name")
if let name = nameObject as? String {
print(name)
}
// let arr = [1, 2, 3, 4]
// UserDefaults.standard().set(arr, forKey: "array")
let arrayObject = UserDefaults.standard.object(forKey: "array")
if let array = arrayObject as? NSArray {
print(array)
}
如果id==数据,只需在内部添加“break”
for data in id {
if listMenu[indexPath.row].id == data{
cellTheme.checkOutlet.image = UIImage(named: "ic_btn_home_checklist_on")
break
}else{
cellTheme.checkOutlet.image = UIImage(named: "ic_btn_home_checklist_off")
}
}
您是否使用CoreData存储检查表信息?这已经很长时间了,但根据我的需要,谢谢