当我的searchBar处于活动状态并且有文本时,我可以删除单元格但是当它被删除时单元格保持空白,我的意思是不再重新加载?这里我有这个代码是我的deleteAction;
if self.searchController.isActive && self.searchController.searchBar.text != "" {
context.delete(filteredArray[indexPath.row])
tableView.reloadData()
self.appDelegate.saveContext()
do { people = try context.fetch(Person.fetchRequest()} catch { print("Fetching Failed !!")}
} else {
context.delete(people[indexPath.row])
self.appDelegate.saveContext()
do { people = try context.fetch(Person.fetchRequest())} catch { print("Fetching Failed !!")}
tableView.reloadData()
}
和cellForRowAt的代码..
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as UITableViewCell!
let person = people[indexPath.row]
if searchController.isActive && searchController.searchBar.text != "" {
cell?.textLabel?.text = filteredArray[indexPath.row].name
if filteredArray[indexPath.row].isChecked {
cell?.accessoryType = .checkmark
} else {
cell?.accessoryType = .none
}
} else {
cell?.textLabel?.text = person.name
if person.isChecked {
cell?.accessoryType = .checkmark
} else {
cell?.accessoryType = .none
}
}
cell?.backgroundColor = UIColor.clear // loading color
return cell!
}
这是截图;
答案 0 :(得分:0)
原因是您从name
filteredArray
if searchController.isActive && searchController.searchBar.text != "" {
cell?.textLabel?.text = filteredArray[indexPath.row].name // this line
但是,您只更新了people
数组:
if self.searchController.isActive && self.searchController.searchBar.text != "" {
context.delete(filteredArray[indexPath.row])
self.appDelegate.saveContext()
// This line below
do { people = try context.fetch(Person.fetchRequest()} catch { print("Fetching Failed !!")}
// Please also update your filteredArray here
filteredArray = ...
// And don't forget to reload after date is updated.
tableView.reloadData()
} else {
...
更新filteredArray
后,它应该有效。