Deploying a UISearchController Using a UITableView

Just think about the code that was used for the Raywenderlich Tutorial on how to add a UISearchController and how to use it with the UITableViewController I can't get it to work, and someone told me it might be deprecated in iOS 8.0 if anyone knows anything about how to do this?

UISearchController been integrated into the UIViewController NOT StoryBoard!

+1
source share
1 answer

UISearchDisplayController deprecated and replaced with UISearchController . And it is available in iOS 8.0 and later.

The UISearchController class defines the interface that controls the presentation of the search bar along with the search results for the contents of the controllers. The search result controller, the UIViewController object specified in the searchResultsController property, controls the search results

https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UISearchController/index.html

Here is an example of how I do this with a UITableView resding in a UIViewController . Just make a few changes if you want to use with the UITableViewController ...

 import UIKit class ViewController: UIViewController , UITableViewDataSource, UITableViewDelegate,UISearchResultsUpdating { @IBOutlet weak var tblView: UITableView! var tabledata = ["lucques","chickendijon","godaddy","amazon","chris","ambata","bankofamerica","abelcine","AUTO + TRANSPORTATION","BILLS + UTILITIES","FOOD + DINING","HEALTH","AutoCare", "Auto Payment" , "Gas+Fuel","Electric Bill", "Internet/Television","Fast Foodd", "Gorceries" , "Restaurants","Gym Membership", "Health Insurance","auto","note-bullet","knife","heart"] var filteredTableData = [String]() var resultSearchController = UISearchController() override func viewDidLoad() { super.viewDidLoad() tblView.delegate = self tblView.dataSource = self self.resultSearchController = ({ let controller = UISearchController(searchResultsController: nil) controller.searchResultsUpdater = self controller.dimsBackgroundDuringPresentation = false controller.searchBar.sizeToFit() controller.searchBar.barStyle = UIBarStyle.Black controller.searchBar.barTintColor = UIColor.whiteColor() controller.searchBar.backgroundColor = UIColor.clearColor() self.tblView.tableHeaderView = controller.searchBar return controller })() self.tblView.reloadData() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1 } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if self.resultSearchController.active { return self.filteredTableData.count }else{ return self.tabledata.count } } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { var section = indexPath.section var row = indexPath.row let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier:"addCategoryCell") cell.selectionStyle = UITableViewCellSelectionStyle.None cell.backgroundColor = UIColor.clearColor() cell.contentView.backgroundColor = UIColor.clearColor() cell.textLabel?.textAlignment = NSTextAlignment.Left cell.textLabel?.textColor = UIColor.blackColor() cell.textLabel?.font = UIFont.systemFontOfSize(14.0) if self.resultSearchController.active { cell.textLabel?.text = filteredTableData[indexPath.row] }else{ cell.textLabel?.text = tabledata[indexPath.row] } return cell } func updateSearchResultsForSearchController(searchController: UISearchController) { filteredTableData.removeAll(keepCapacity: false) let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text) let array = (tabledata as NSArray).filteredArrayUsingPredicate(searchPredicate) filteredTableData = array as! [String] self.tblView.reloadData() } } 
+4
source

Source: https://habr.com/ru/post/989343/


All Articles