使用UITableView实现UISearchController

时间:2015-06-15 17:35:44

标签: xcode swift uitableview uisearchcontroller

只是想知道用于Raywenderlich Tutorial的代码如何添加UISearchController以及如何将其与UITableViewController一起使用我似乎无法使其正常运行有人告诉我,它可能已经在iOS 8.0中被弃用,有人知道如何仍然这样做吗?

UISearchController内置于UIViewController NOT StoryBoard!

1 个答案:

答案 0 :(得分:5)

UISearchDisplayController已弃用并由UISearchController取代。并且可在 iOS 8.0 及更高版本中使用。

  

UISearchController类定义了一个管理它的接口   与搜索结果一致地显示搜索栏   控制器的内容。搜索结果控制器,a   由searchResultsController指定的UIViewController对象   property,管理搜索结果

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

以下是一个示例,我如何使用UITableView中的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()



    }



}