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

问题描述:

我在我的tableview应用程序中实现了一个搜索功能。唯一的问题是当我点击搜索栏,然后点击取消,先前显示的单元格消失。这是全部股票;什么也没有变。当我添加搜索显示控制器时,我从故事板中添加了它,但没有配置。当搜索栏被点击时,Tableviewcells消失 - Swift

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

+0

你在你的代码某处的错误。发布显示/隐藏搜索栏的代码以及过滤数据的代码。 –

+0

@DuncanC就是这样。没有代码已经建立。我只是从故事板中的对象库中将它添加到我的tableview中。你是否推荐任何资源来学习如何设置它? –

+0

http://shrikar.com/swift-ios-tutorial-uisearchbar-and-uisearchbardelegate/点击此链接 – karthik

你将要实现的UISearchControllerDelegate方法:didDismissSearchController(searchController:UISearchController)

当你搜索控制器被解雇这就是所谓的。在这里你可以重新加载你的tableView数据。

,你可以在这里阅读更多:https://developer.apple.com/reference/uikit/uisearchcontrollerdelegate

我刚才改变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 
})() 

```