小编典典

使用UITableView实现UISearchController

swift

我只是想知道有关Raywenderlich教程的代码,该代码如何添加UISearchController以及如何与a一起使用?UITableViewController我似乎无法正常工作,有人告诉我它可能已在iOS
8.0中弃用,有人知道吗?关于如何仍然这样做?

UISearchController始建于UIViewControllerNOT脚本!


阅读 239

收藏
2020-07-07

共1个答案

小编典典

UISearchDisplayController已被弃用,并由代替UISearchController。它在 iOS 8.0
及更高版本中可用。

UISearchController类定义一个接口,该接口与搜索结果控制器的内容一致地管理搜索栏的显示。搜索结果控制器(由searchResultsController属性指定的UIViewController对象)管理搜索结果

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

这是一个示例,我如何使用UITableView.s中的UIViewController代码进行修改。如果要与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()



    }



}
2020-07-07