当前位置: 首页 > 面试题库 >

使用UITableView实现UISearchController

师曦
2023-03-14
问题内容

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

UISearchController始建于UIViewControllerNOT脚本!


问题答案:

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



    }



}


 类似资料:
  • 问题内容: 我的 TapCell1.swift 这是习惯 我的 ViewController.swift 正确设置了其所有数据源和代理。但是未显示我的自定义单元格。 问题是我的自定义单元格未显示。请提出我做错的任何事情。 注意:这是我的代码 我的文件下载链接 问题答案: 我终于做到了。 对于TapCell1.swift 对于NextViewController.swift 我的工作代码链接:自定义

  • 本文向大家介绍iOS UITableView 拖动排序实现代码,包括了iOS UITableView 拖动排序实现代码的使用技巧和注意事项,需要的朋友参考一下 UITbableView作为列表展示信息,除了展示的功能,有时还会用到删除,排序等功能,下面就来讲解一下如何实现排序。  排序是当表格进入编辑状态后,在单元格的右侧会出现一个按钮,点击按钮,就可以拖动单元格,移动位置,进行手动排序。  使用

  • 我有一个视图控制器,其中包含一个表视图,所以我想问我应该把表视图数据源和委托放在哪里,它应该是一个外部对象,或者如果我们说VIPER模式,我可以把它写在我的视图控制器中。 Presenter包含interactor,在showSongs方法中,我从interactor请求一些数据,比如:self.interactor.loadSongs() 当歌曲准备好返回到view controller时,我再

  • 所以我在这里读到,我可以简单地将一个UITableViewCell拖到情节提要中的UITableViewController的底部,让它像一个页脚一样。这个页脚行中有一个活动指示器,仅此而已。它的宽度是320,高度是55,指示器在行中居中。请注意,它是一个UIView而不是UITableViewCell,因为我只能让它与前者一起工作。 首先,UITableView不会停留在页脚。如果将滚动扩展到U

  • UITableView-Wave 使用波状效果实现 UITableView 重载的动画效果。    

  • UITableView-AnimationControl 是控制 UITableView 行/区间更新 持续时间和完结的方法。代码示例 [self.tableViewDataArray addObject:object];[self.tableView beginSmartUpdatesForDuration:0.25];[self.tableView insertRowsAtIndexPaths