Swift UiTableView 不重新加载搜索结果

标签 swift xcode

我有一个奇怪的问题,由于某种原因,我的 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/

相关文章:

swift - 在 Swift Storyboards for mac 中从一个 NSViewController 切换到另一个 NSViewController

swift - ARKit – 光估计

swift - Swift 中从 userInfo 获取键盘大小

ios - 无法访问函数外定义的字典

ios - 从未使用有效格式解析的字节加载的Swift DateFormatter字符串

ios - 如何使布局保持不变,但根据设备增加/减小尺寸?

iPhone - 将字符串截断到最后四位

ios - 应用程序调试输出保存到 iOS 模拟器文件系统的哪里

iphone - 当函数被延迟调用时,我得到了错误的访问异常

ios - 如何在 Xcode 中打开 Expo React Native 项目以创建构建