gpt4 book ai didi

Swift UiTableView 不重新加载搜索结果

转载 作者:可可西里 更新时间:2023-11-01 02:00:54 27 4
gpt4 key购买 nike

我有一个奇怪的问题,由于某种原因,我的 UITableView 在执行搜索后没有重新加载。控制台打印出正确过滤的数据,但表格根本没有改变。我从来没有遇到过这个问题,所以我首先尝试了自然想到的解决方案:

  1. 在主队列中尝试了 tableView.reloadData()

  2. 退出 Xcode,清理构建,重新安装

  3. 清除派生数据目录

我在 SO 中发现了几个类似的问题,但我看到的所有解决方案都是我尝试过的,主要是在主队列中重新加载 tableview。

希望也许我只是在我的代码中遇到了问题或者我遗漏了什么。

我正在运行 Xcode 8.3.3

import UIKit

class CategoriesViewController: UIViewController {

var isFiltering = false
var location = Location()
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var searchBar: UISearchBar!
var categoriesSearchResults = [Category]()

override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
tableView.allowsSelection = true
tableView.keyboardDismissMode = .onDrag
let nib = UINib(nibName: "CategoryTableViewCell", bundle: nil)
self.tableView.register(nib, forCellReuseIdentifier:"CategoryTableViewCell");
searchBar.returnKeyType = UIReturnKeyType.done
searchBar.autocapitalizationType = .none
searchBar.delegate = self
}


extension CategoriesViewController : UITableViewDelegate, UITableViewDataSource {

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 60
}

func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("HI")
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

if isFiltering {
return self.categoriesSearchResults.count
}

return self.location.categories.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
if let cell = self.tableView.dequeueReusableCell(withIdentifier: "CategoryTableViewCell", for: indexPath) as? CategoryTableViewCell {
var category: Category
if isFiltering {
category = self.categoriesSearchResults[indexPath.row]
} else {
category = self.location.categories[indexPath.row]
}
cell.name.text = category.name
cell.status.textColor = UIColor.lightGray
cell.status.text = "Not Verified"
}

return cell
}
}

extension CategoriesViewController : UISearchBarDelegate {

func searchBarIsEmpty() -> Bool{
return self.searchBar.text?.isEmpty ?? true
}

func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
self.isFiltering = true
self.categoriesSearchResults.removeAll()
tableView.reloadData()
self.view.endEditing(true)
}

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
if searchBarIsEmpty() {
self.view.endEditing(true)
self.isFiltering = false
} else {
self.isFiltering = true
self.categoriesSearchResults = self.location.categories.filter({ (category: Category) -> Bool in
return category.name.lowercased().contains(searchText.lowercased())
})
}
tableView.reloadData()
}
}

和我的自定义表格 View 单元格:

import UIKit

class CategoryTableViewCell: UITableViewCell {
@IBOutlet weak var name: UILabel!
@IBOutlet weak var status: UILabel!

override func awakeFromNib() {
super.awakeFromNib()

}

override func prepareForReuse() {
super.prepareForReuse()
self.name.text = ""
self.status.text = ""
}

override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)

// Configure the view for the selected state
}

}

提前谢谢你。

编辑:可能还值得一提的是,当我积极搜索时,函数 tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 没有被调用?

最佳答案

if let 的作用域嵌套在它的作用域中。在您的代码中,您总是返回 let cell = UITableViewCell()。尝试在 if let 中返回它:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
if let cell = self.tableView.dequeueReusableCell(withIdentifier: "CategoryTableViewCell", for: indexPath) as? CategoryTableViewCell {
var category: Category
if isFiltering {
category = self.categoriesSearchResults[indexPath.row]
} else {
category = self.location.categories[indexPath.row]
}
cell.name.text = category.name
cell.status.textColor = UIColor.lightGray
cell.status.text = "Not Verified"

/// RETURN CELL HERE
return cell

}

return cell
}

关于Swift UiTableView 不重新加载搜索结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46399696/

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