当点击搜索栏时,Tableviewcells消失 - Swift

时间:2016-05-21 13:26:21

标签: ios swift uitableview tableview uisearchdisplaycontroller

我在tableview应用程序中实现了搜索功能。唯一的问题是当我点击搜索栏,然后点击取消时,之前显示的单元格已经消失。这都是库存;什么也没有变。当我添加搜索显示控制器时,我在故事板中添加了它而没有配置。

Before Search is tapped

import UIKit

class DataTableExercisesTableViewController: UITableViewController, UISearchResultsUpdating {

let exercises = ["Abs", "Arms", "Back", "Chest", "Legs", "Shoulders", "Triceps"]
var filteredTableData = [String]()
var resultSearchController = UISearchController(searchResultsController: nil)





override func viewDidLoad() {
    super.viewDidLoad()

    self.resultSearchController = ({
        let controller = UISearchController(searchResultsController: nil)
        resultSearchController.searchResultsUpdater = self
        resultSearchController.dimsBackgroundDuringPresentation = false
        resultSearchController.searchBar.sizeToFit()
        resultSearchController.hidesNavigationBarDuringPresentation = false
        tableView.tableHeaderView = resultSearchController.searchBar


        return controller
    })()

    self.tableView.reloadData()

}



// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    // #warning Incomplete implementation, return the number of sections

    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if resultSearchController.active {
        return filteredTableData.count
    } else {
        return exercises.count
    }
}





override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("ExerciseCell", forIndexPath: indexPath)



    // Configure the cell...

    if (self.resultSearchController.active) {
        cell.textLabel?.text = filteredTableData[indexPath.row]

        return cell
    }
    else {
        cell.textLabel?.text = exercises[indexPath.row]

        return cell
    }
}


 override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

self.tableView.reloadData()


}

func updateSearchResultsForSearchController(searchController: UISearchController)
{
    filteredTableData.removeAll(keepCapacity: false)

    let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text!)
    let array = (exercises as NSArray).filteredArrayUsingPredicate(searchPredicate)
    filteredTableData = array as! [String]

    self.tableView.reloadData()
}

After Search is tapped

2 个答案:

答案 0 :(得分:0)

您将需要实现UISearchControllerDelegate方法:didDismissSearchController(searchController:UISearchController)

当您的搜索控制器被解除时,会调用此方法。在这里,您可以重新加载tableView数据。

您可以在此处阅读更多内容:https://developer.apple.com/reference/uikit/uisearchcontrollerdelegate

答案 1 :(得分:0)

我刚刚更改了resultSearchController的初始化,如下所示,然后就可以了。

self.resultSearchController = ({
    let controller = UISearchController(searchResultsController: nil)
    controller.searchResultsUpdater = self
    controller.dimsBackgroundDuringPresentation = false
    controller.searchBar.sizeToFit()
    controller.hidesNavigationBarDuringPresentation = false
    tableView.tableHeaderView = controller.searchBar

    return controller
})()

```