【问题标题】:iOS UITableView doesn't reload data when searchingiOS UITableView 搜索时不重新加载数据
【发布时间】:2017-08-03 02:23:05
【问题描述】:

我正在实现一个类似 Instagram 的搜索页面 - 这样当您转到搜索选项卡时,您会看到趋势搜索(带有动态部分)等,但是当您开始在搜索框中输入时,趋势搜索消失并且搜索结果显示。

我就是这样做的,但它似乎不起作用。 A)当我搜索某些东西时没有显示任何结果(我记录了 api 调用的响应 - 我肯定正确地获取了数据)。 B)即使在我点击取消后,我也不会返回显示趋势结果(再次,我在 cancelSearch 操作中打印,该函数肯定会被调用)

这是我的代码的简化版本:

class SearchVC: UITableViewController {

let searchController = UISearchController(searchResultsController: nil)

var sections = [String]()
var allTrendingOfferings = [[OfferingModel]]()
var allSearchResultOfferings = [OfferingModel]()

override func viewDidLoad() {
    super.viewDidLoad()

    sections = // data from backend
    allTrendingOfferings = // data from backend

    searchController.searchResultsUpdater = self
    searchController.dimsBackgroundDuringPresentation = false
    definesPresentationContext = true
    tableView.tableHeaderView = searchController.searchBar
}

// MARK: - Table view data source
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    if (searchController.isActive) {
        return ""
    }
    return self.sections[section]
}

override func numberOfSections(in tableView: UITableView) -> Int {
    if (searchController.isActive) {
        return 1
    }
    return self.sections.count
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if (searchController.isActive) {
        return allSearchResultOfferings.count
    }
    return allTrendingOfferings[section].count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "offeringCell", for: indexPath) as! SeachTVCell

    if (searchController.isActive) {
        let offering = allSearchResultOfferings[indexPath.row]
    } else {
        let offering = allTrendingOfferings[indexPath.section][indexPath.row]
    }
    // configure cell and
    return cell
}

func filterContentForSearchText(searchText: String, scope: String = "All") {
    allSearchResultOfferings = // data fetched from backend
    self.tableView.reloadData() // done asynchronously (after receiving data)
}

func searchCancel() {
    self.tableView.reloadData()
}

}

extension SearchVC: UISearchResultsUpdating {
    @available(iOS 8.0, *)
    func updateSearchResults(for searchController: UISearchController){
        if !searchController.isActive {
            searchCancel()
        }
        filterContentForSearchText(searchText:searchController.searchBar.text!)
    }
}

【问题讨论】:

  • 您需要在filterContentForSearchText中显示代码。最可能的原因是由于调用的异步性质,reloadData 在更新 allSearchResultOfferings 之前被调用。
  • 你应该在主线程中调用reloadData()DispatchQueue.main.async { tableView. reloadData() }
  • @NandiinBao 是的,一旦我从 api 调用中得到响应,我就会在主线程中调用 reloadData()!
  • @Michael 谢谢大家!我修好了...我只需要退出 xcode,清理并重建项目...花了大约 5 个小时!

标签: ios swift uitableview uisearchbar uisearchcontroller


【解决方案1】:

误报 - 不得不退出 Xcode,清理并重建项目。它现在就像一个魅力。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多