【问题标题】:Why is my tableview blank after using UISearchResultsUpdating cancel button?为什么使用 UISearchResultsUpdating 取消按钮后我的 tableview 是空白的?
【发布时间】:2015-06-14 04:28:58
【问题描述】:

我有一个表格视图,它加载一个函数并在 viewDidAppear 中显示数据。当用户点击导航标题中的 searchBar 并搜索查询时,使用相同的 tableview(我假设)。

问题是用户点击取消按钮后:调用原始数据的函数执行并打印正确的数据,但在tableView.reloadData之后没有出现在屏幕上。

我尝试将函数放在不同的地方(didCancel、didEndEditing),函数被调用/正确返回数据,但没有出现在表格上。

有什么建议或解决方法吗?

class LocationViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchResultsUpdating, UISearchBarDelegate, UISearchControllerDelegate {

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.delegate = self
    tableView.dataSource = self
}

override func viewWillAppear(animated: Bool) {
        self.load0()
        tableView.reloadData()

    self.locationSearchController = ({
        let controller = UISearchController(searchResultsController: nil)
        controller.searchResultsUpdater = self
        controller.hidesNavigationBarDuringPresentation = false
        controller.dimsBackgroundDuringPresentation = false
        controller.searchBar.delegate = self
        controller.searchBar.searchBarStyle = .Minimal
        controller.searchBar.sizeToFit()
        self.navigationItem.titleView = controller.searchBar
        return controller
    })()
}

//below I try to remove the search data and reload the original data.
override func viewDidDisappear(animated: Bool) {
    self.locationSearchController.active = false
}

func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
    self.searchData.removeAllObjects()
    self.category.removeAll(keepCapacity: false)
    self.product.removeAll(keepCapacity: false)
    self.users.removeAll(keepCapacity: false)
self.load0()
    tableView.reloadData()
}


func searchBarTextDidEndEditing(searchBar: UISearchBar) {
    self.searchData.removeAllObjects()
    self.category.removeAll(keepCapacity: false)
    self.product.removeAll(keepCapacity: false)
    self.users.removeAll(keepCapacity: false)
self.load0()
tableView.reloadData()

}

func searchBarCancelButtonClicked(searchBar: UISearchBar) {
self.searchData.removeAllObjects()
    self.category.removeAll(keepCapacity: false)
    self.product.removeAll(keepCapacity: false)
    self.users.removeAll(keepCapacity: false)
       self.load0()
        tableView.reloadData() }

执行搜索的函数是updateSearchResultsForSearchController:

func updateSearchResultsForSearchController(searchController: UISearchController) { query and places items in an array that's displayed by the original tableView }

我想知道是否使用了我不知道的 tableView,但很明显 searchController 正在使用原始 tableView,因为它遵循我的可重用单元格的设计。

【问题讨论】:

  • 你能贴一些代码吗?

标签: ios swift uitableview uisearchbar


【解决方案1】:

你必须以这种方式填充你的 UITableView:

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if self.searchDisplayController!.active {
            return self.filteredData.count
        }
        return self.defaultdData.count
    }

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell = self.tableView.dequeueReusableCellWithIdentifier("Cell") as! UITableViewCell

        if self.searchDisplayController!.active {
            cell.textLabel.text = self.filteredData[indexPath.row]
        } else {
            cell.textLabel.text = self.defaultData[indexPath.row]
        }

        return cell
    }

【讨论】:

    猜你喜欢
    • 2014-10-19
    • 2017-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-06
    • 1970-01-01
    • 2018-03-02
    • 2013-03-13
    相关资源
    最近更新 更多