UISearchDisplayController

//
//  FirstViewController.swift
//  SearchDisplayDemo
//
//  Created by Bruce Lee on 24/12/14.
//  Copyright (c) 2014 Dynamic Cell. All rights reserved.
//

import UIKit

class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchDisplayDelegate, UISearchBarDelegate {

    @IBOutlet weak var tableView: UITableView!
    var searchBar: UISearchBar!
    var searchController: UISearchDisplayController!
    var maskView: UIVisualEffectView!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.searchBar = UISearchBar(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, 44))
        searchBar.delegate = self;
        self.tableView.tableHeaderView = self.searchBar
        self.searchController = UISearchDisplayController(searchBar: self.searchBar, contentsController: self)
        self.searchController.delegate = self
        var blurEffect = UIBlurEffect(style: .Light)
        maskView = UIVisualEffectView(effect: blurEffect)
        maskView.frame = UIScreen.mainScreen().bounds

        var testLabel = UILabel(frame: CGRectMake(100, 100, 100, 30))
        testLabel.text = "hello world"
        maskView.addSubview(testLabel)
    }

    // MARK: - UISearchBar delegate
    func searchBarShouldBeginEditing(searchBar: UISearchBar) -> Bool {
        println("search")
//        self.searchDisplayController?.searchResultsTableView.backgroundView = searchBackgroundView
        self.view.addSubview(maskView)
        return true
    }

    // MARK: - UISearchDisplayController delegate
    func searchDisplayController(controller: UISearchDisplayController, willHideSearchResultsTableView tableView: UITableView) {
        maskView.removeFromSuperview()
    }

    func searchDisplayControllerWillEndSearch(controller: UISearchDisplayController) {
        println("1")
        maskView.removeFromSuperview()
    }

    func searchDisplayControllerDidEndSearch(controller: UISearchDisplayController) {
        println("2")
    }

    // MARK: - UITableView delegate & datasour

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as? UITableViewCell
        if cell == nil {
            cell = UITableViewCell(style: .Default, reuseIdentifier: "Cell")
        }
        if indexPath.row % 2 == 0{
            cell?.contentView.backgroundColor = UIColor.blueColor()
        }
        else{
            cell?.contentView.backgroundColor = UIColor.greenColor()
        }
        cell?.textLabel?.text = "\(indexPath.row)"

        return cell!
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
时间: 2024-07-30 13:49:52

UISearchDisplayController的相关文章

ios UISearchDisplayController 实现 UITableView 搜索功能

UISearchDisplayController 是苹果专为 UITableView 搜索封装的一个类. 里面内置了一个 UITableView 用于显示搜索的结果.它可以和一个需要搜索功能的 controller 关联起来,其它的像原 TableView 和搜索结果 TableView 的切换, mask 的显示等等都 封装好了,使用起来非常非常的简单.特别是要实现全屏搜索时使用最多. 全屏搜索的意思是如果你用了  NavigationBar 当点击搜索框时 TableView 会自动弹上去

UISearchBar和UISearchDisplayController

UISearchBar和UISearchDisplayController实例应用 程序介绍:获取系统通讯录,利用 UISearchBar和UISearchDisplayController实现搜索功能 1 #import <UIKit/UIKit.h> 2 3 @interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate,UISearc

iOS --- 搜索框UISearchController的使用(iOS8.0以后替代UISearchBar + UISearchDisplayController的组合)

在iOS 8.0以上版本中, 我们可以使用UISearchController来非常方便地在UITableView中添加搜索框. 而在之前版本中, 我们还是必须使用UISearchBar + UISearchDisplayController的组合方式. 添加UISearchController属性: @property(strong, nonatomic) UISearchController *searchController; @property(strong, nonatomic) NS

UISearchDisplayController 使用

1.首先定义属性 @property (nonatomic, retain) UISearchDisplayController *searchDisplayController; 2.创建一个UISearchBar UISearchBar *search = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40)]; [search setPlaceholder:@"搜索"

iOS之iPhone手机通讯录和短信搜索界面的实现以及UISearchController和UISearchDisplayController的浅析

本来觉得这个模块也就是一个SearchBar就搞定了,但是现在的产品经理也是够了,一会儿一个想法,之前的搜索 都已经写完了,类似主流的电商,好像也没那么麻烦,但是改版了总得弄点什么吧.嘿,哥们,我现在要iphone手机 通讯录里面搜索的样式,你搞定哦......,要一毛一样哦.作为一个文化人,我只能在内心深处生 出表达出,苦逼的我们顶多发发牢骚,要改就改喽. 请看图先 这是他要的效果demo 下面是我写的demo 看到这效果,应该都能想到用UISearchController,但是这货是iOS8

iOS中的2中搜索方式UISearchDisplayController和UISearchController

大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 以前iOS的搜索一般都使用UISearchDisplayController来完成,不过自从iOS 8.0开始,该控制器被标记为废弃,我们可以在iOS 8.0之后使用一个新的搜索控制器UISearchController来完成搜索. 本篇博文将介绍以上2种搜索控制器的简单用法,并比较它们的区别.Let't Go! UISearchDisplayControll

UISearchBar和 UISearchDisplayController的使用

上面是一个navigationController,接下来一个searchBar,下面是tableView searchBar这个控件就用来搜索tableView上的数据 [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self]; UISearchDisplayController这个控件很强大,它初始化是基于searchBar的,里面有些效果很不错,apple都封装好了,并且可

点击搜索取消UISearchDisplayController的搜索状态

一般,我们用到UISearchDisplayController的时候,都是须要对一个数据源进行刷选,在UISearchDisplayController自带的tableView中展示出来,然后点击退出详情.我近期在做大众点评第三方的时候,遇到一个问题,我展示出了所有商店,想加搜索功能,可是不知道输入的搜索keyword去跟什么匹配,大众点评也并没有提供一个用来刷选的数据源接口,这样,我仅仅能自己输入keyword,不用它数据源提供刷选补全内容,而直接点击弹出键盘的搜索键,进行搜索.只是点击搜索

UISearchDisplayController搜索(iOS8前)

搜索功能,基本每个app标配. 实现起来很简单,但是iOS8后苹果建议使用UISearchController,官方Demo:Table Search with UISearchController 实际开发基本也都还是用的老的UISearchDisplayController+UISearchBar的方案,因为要照顾一些版本低的用户. 发现时间长没写都忘记差不多了,闲暇之余,一起整理下,方便以后翻阅. 这篇先从UISearchDisplayController开始.比较简单,就不罗嗦了,直接贴