【问题标题】:Implement UISearchController with UITableView用 UITableView 实现 UISearchController
【发布时间】:2015-06-15 17:35:44
【问题描述】:

只是想知道用于Raywenderlich Tutorial 的代码如何添加UISearchController 以及如何将它与UITableViewController 一起使用我似乎无法让它工作,有人告诉我它可能已在 iOS 8.0 中弃用,有人知道如何继续这样做吗?

UISearchController 内置于 UIViewController 而不是故事板!

【问题讨论】:

    标签: xcode swift uitableview uisearchcontroller


    【解决方案1】:

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

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

    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()
    
    
    
        }
    
    
    
    }
    

    【讨论】:

    • 还有一种方法可以添加它,以便在显示 UISearchController 时仍显示单元格,因为此时单元格不会显示,直到您输入要搜索的名称,否则谢谢! :D @anishparajuli
    猜你喜欢
    • 1970-01-01
    • 2015-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多