使用 UITableView 实现 UISearchController [英] Implement UISearchController with UITableView

查看:20
本文介绍了使用 UITableView 实现 UISearchController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是想知道用于 Raywenderlich 教程 关于如何添加 UISearchController 以及如何将它与 UITableViewController 一起使用我似乎无法让它工作,有人告诉我它可能已被弃用iOS 8.0,有人知道怎么做吗?

Just wondering about the code that was used for the Raywenderlich Tutorial on how to add a UISearchController and how to use it with a UITableViewController I can't seem to get it working and someone told me that it may have got deprecated in iOS 8.0, does anyone know on how to still do this?

UISearchController 内置于 UIViewController 不是 StoryBoard!

The UISearchController was Built In The UIViewController NOT StoryBoard!

推荐答案

UISearchDisplayController 已被弃用并替换为 UISearchController.它在 iOS 8.0 中可用 及以后.

UISearchDisplayController has been deprecated and replaced by UISearchController.And it is available in iOS 8.0 and later.

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

The UISearchController class defines an interface that manages the presentation of a search bar in concert with the search results controller’s content. The search results controller, a UIViewController object specified by the searchResultsController property, manages the results of the search

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

这是一个例子,我是如何使用 UITableViewUIViewController 中实现的.如果你想与 UITableViewController 一起使用,只需做一些更改>...

Here is an example ,how i do it with UITableView resding in UIViewController..Just make few changes if you want to use with 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()



    }



}

这篇关于使用 UITableView 实现 UISearchController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆