gpt4 book ai didi

swift - 当searcBar有文本时,我无法重新加载tableView

转载 作者:行者123 更新时间:2023-11-30 12:55:29 24 4
gpt4 key购买 nike

当我的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!
}


这是屏幕截图;

enter image description here
enter image description here

最佳答案

原因是因为您要从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后,它应该可以工作。

关于swift - 当searcBar有文本时,我无法重新加载tableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40451580/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com