我有一个UITableView
in one UIViewController
,我尝试存储附件类型的状态,以便当用户重新加载应用程序时,用户预先选择使用的单元格NSUserDefault
应该显示一个复选标记。但我面临的问题是,当我检查单元格的数据是否等于用户默认值中的数据时,一些未选中的单元格也会显示一个复选标记,但它不应该那样做。
这是我的代码:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = newCategoriesTableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.backgroundColor = colorArray[indexPath.row]
let entry: Categories
if isFiltering() {
entry = filteredCategories[indexPath.row]
} else {
entry = categoryTitles[indexPath.row]
}
cell.selectionStyle = .none
cell.textLabel?.text = entry.name
cell.detailTextLabel?.text = entry.description
let defaults = UserDefaults.standard
if let userCategortList = defaults.object(forKey: "userCategoryList") as? [String]{
for category in userCategortList{
if(cell.textLabel?.text == category){
cell.accessoryType = .checkmark
break
}
}
}
return cell
}