UI-UISEARCHCONTROLLER&UISEARCHDISPLAYCONTROLLER__ Search

Source: Internet
Author: User
Brief Introduction

System with the Search page class-uisearchdisplaycontroller and Uisearchcontroller detailed use of the method, so that you more convenient and efficient search function development. The effect is as follows:
Uisearchdisplaycontroller

Ns_class_deprecated_ios (3_0, 8_0, "Uisearchdisplaycontroller has been replaced with Uisearchcontroller") 1. Take a look at the attributes and agents that are used first

@interface SEARCHDISPLAYVC () <uitableviewdatasource,uitableviewdelegate,uisearchbardelegate, Uisearchdisplaydelegate>
@property (nonatomic, strong) Uisearchdisplaycontroller *searchdisplaycontroller;
@property (nonatomic, strong) UITableView *tableview;
@property (nonatomic, strong) UITableView *resulttableview;
@property (nonatomic, strong) Nsarray *dataarr;
@property (nonatomic, strong) Nsarray *resultarr;
@end
-(Nsarray *) createdata {Nsarray *dataarr = @[@ "Aaliyah", @ "Aaron", @ "Abigail", @ "Adam", @ "Addison", @ "Adrian", @ "Aid En ", @" Alex ", @" Alexa ", @" Alexander ", @" Alexandra ", @" Alexis ", @" Allison ", @" Alyssa ", @" Amelia ", @" Andrea ", @" Andrew ", @" Angel ", @" Anna ", @" Annabelle ", @" Anthony ", @" Aria ", @" Ariana ", @" Arianna ", @" Ashley ", @" Aubree ", @" Aubrey ", @" Audrey ", @" Austin ", @" Autumn ", @" Ava ", @" Avery ", @" Ayden ", @" Bailey ", @" Bella ", @" Benjamin ", @" Bentley ", @" Blake ", @" Brandon ", @" Bra " Yden ", @" Brianna ", @" Brody ", @" Brooklyn ", @" Bryson ", @" Caleb ", @" Cameron ", @" Camila ", @" Carlos ", @" Caroline ", @" Carson ", @ "Carter", @ "Charles", @ "Charlotte", @ "Chase", @ "Chloe", @ "Christian", @ "Christopher", @ "Claire", @ "Colton", @ "Connor", @ "Cooper", @ "Damian", @ "Daniel", @ "David", @ "Dominic", @ "Dylan", @ "Easton", @ "Eli", @ "Elijah", @ "Elizabeth", @ "Ella", @ "E"  Llie ", @" Emily ", @" Emma ", @" Ethan ", @" Eva ", @" Evan ", @" Evelyn ", @" Faith ", @" Gabriel ", @" Gabriella ", @" Gavin ", @" Genesis ", @ "Gianna", @ "Grace", @ "Grayson"@ "Hailey", @ "Hannah", @ "Harper", @ "Henry", @ "Hudson", @ "Hunter", @ "Ian", @ "ISAAC", @ "Isabella", @ "Isaiah", @ "Jace", @ "Ja" CK ", @" Jackson ", @" Jacob ", @" James ", @" Jasmine ", @" Jason ", @" Jaxon ", @" Jayden ", @" Jeremiah ", @" Jocelyn ", @" John ", @" Jonat " Han ", @" Jordan ", @" Jose ", @" Joseph ", @" Joshua ", @" Josiah ", @" Juan ", @" Julia ", @" Julian ", @" Justin ", @" Katherine ", @" Kayde " n ", @" Kayla ", @" Kaylee ", @" Kennedy ", @" Kevin ", @" Khloe ", @" Kimberly ", @" Kylie ", @" Landon ", @" Lauren ", @" Layla ", @" Leah ", @ "Levi", @ "Liam", @ "Lillian", @ "Lily", @ "Logan", @ "London", @ "Lucas", @ "Lucy", @ "Luis", @ "Luke", @ "Lydia", @ "Mackenzie", @ " Madeline ", @" Madelyn ", @" Madison ", @" Makayla ", @" Mason ", @" Matthew ", @" Maya ", @" Melanie ", @" Mia ", @" Michael ", @" Molly ", @ "Morgan", @ "Naomi", @ "Natalie", @ "Nathan", @ "Nathaniel", @ "Nevaeh", @ "Nicholas", @ "Noah", @ "Nolan", @ "Oliver", @ "Olivia" @ "Owen", @ "Parker", @ "Peyton", @ "Piper", @ "Reagan", @ "Riley", @ "Robert", @ "Ryan", @ "Ryder", @ "Samantha", @ "Samuel", @ "Sa" Rah ", @" Savannah ", @" Scarlett@ "Sebastian", @ "Serenity", @ "Skylar", @ "Sofia", @ "Sophia", @ "Sophie", @ "Stella", @ "Sydney", @ "Taylor", @ "Thomas", @ "Tr"
    Inity ", @" Tristan ", @" Tyler ", @" Victoria ", @" Violet ", @" William ", @" Wyatt ", @" Xavier ", @" Zachary ", @" Zoe ", @" Zoey "];
return Dataarr; }
2. Create TableView and Uisearchdisplaycontroller for the current page
-(void) Configuretableview
{
    Self.dataarr = [self create createdata];
    Self.tableview = [[UITableView alloc]initwithframe:cgrectmake (0, 375, m) Style:uitableviewstyleplain];
    _tableview.delegate = self;
    _tableview.datasource = self;
    [Self.view Addsubview:_tableview];
    [Self Configuretableview:_tableview];
}

-(void) Addsearchbarandsearchdisplaycontroller
{
    Uisearchbar *searchbar = [[Uisearchbar alloc] init];
    [Searchbar SizeToFit];
    Searchbar.delegate = self;
    Self.tableView.tableHeaderView = Searchbar;

    Self.searchdisplaycontroller = [[Uisearchdisplaycontroller alloc] Initwithsearchbar:searchbar contentsController: Self];
    _searchdisplaycontroller.delegate = self;
    _searchdisplaycontroller.searchresultsdatasource = self;
    _searchdisplaycontroller.searchresultsdelegate = self;
}
3. Tableviewdatasource and Tableviewdelegate
//=============================================== #pragma mark-#pragma mark UITableView/ ===============================================-(Nsinteger) TableView: (UITableView *) TableView
    Numberofrowsinsection: (Nsinteger) Section {if ([TableView Isequal:_tableview]) {return self.dataArr.count;
return self.resultArr.count; }-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) IndexPath {UITableVi

    Ewcell *cell = [TableView dequeuereusablecellwithidentifier:@ "Cellid" Forindexpath:indexpath];
    if ([TableView Isequal:_tableview]) {cell.textLabel.text = _dataarr[indexpath.row];
    }else{cell.textLabel.text = _resultarr[indexpath.row];
} return cell; }-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) Indexpath {[TableView deselectRo
Watindexpath:indexpath Animated:yes]; }
4.UISearchDisplayDelegate
=============================================== #pragma mark-#pragma mark Uisearchdisplaydelegate//============= ==================================-(void) Searchdisplaycontrollerwillbeginsearch: (Uisearchdisplaycontroller *)  Controller {NSLog (@ ' would begin search ');}-(void) Searchdisplaycontrollerdidbeginsearch: (Uisearchdisplaycontroller *) Controller {NSLog (@ "did begin Search");}-(void) Searchdisplaycontrollerwillendsearch: (Uisearchdisplaycontrolle  R *) Controller {NSLog (@ "would end search");}-(void) Searchdisplaycontrollerdidendsearch: (Uisearchdisplaycontroller *) Controller {NSLog (@ "did end Search");}-(void) Searchdisplaycontroller: (Uisearchdisplaycontroller *) controller
    Didloadsearchresultstableview: (UITableView *) TableView {NSLog (@ "Did load table");
[Self configuretableview:tableview]; }-(void) Searchdisplaycontroller: (Uisearchdisplaycontroller *) controller Willunloadsearchresultstableview: ( UITableView *) TableView {NSLog (@)Unload table "); }-(void) Searchdisplaycontroller: (Uisearchdisplaycontroller *) controller Willshowsearchresultstableview: ( UITableView *) TableView {NSLog (@ "'ll show Table");}-(void) Searchdisplaycontroller: (Uisearchdisplaycontroller *) Controller Didshowsearchresultstableview: (UITableView *) TableView {NSLog (@ "did show Table");}-(void) Searchdispla Ycontroller: (Uisearchdisplaycontroller *) controller Willhidesearchresultstableview: (UITableView *) TableView {NSLog
(@ "would hide table"); }-(void) Searchdisplaycontroller: (Uisearchdisplaycontroller *) controller Didhidesearchresultstableview: ( UITableView *) TableView {NSLog (@ "did hide Table");}-(BOOL) Searchdisplaycontroller: (Uisearchdisplaycontroller *) c Ontroller shouldreloadtableforsearchstring: (NSString *) searchstring {NSLog (@ should reload table for search string?)

    ");
    Nspredicate *predicate = [nspredicate predicatewithformat:@ "SELF CONTAINS[CD]%@", searchstring]; Self.resultarr = [Self.dataarrFilteredarrayusingpredicate:predicate];
return YES; }-(BOOL) Searchdisplaycontroller: (Uisearchdisplaycontroller *) controller shouldreloadtableforsearchscope: (
    Nsinteger) searchoption {NSLog (@ "should reload table for search scope?");
return YES; }


Uisearchcontroller

In the Uisearchcontroller example, a separate controller is passed in, and the Uisearchdisplaycontroller example is a tableview in the current controller.

#import "SearchVC.h" #import "DCSearchResultTVC.h" @interface SEARCHVC () <uisearchcontrollerdelegate, Uisearchresultsupdating,uisearchbardelegate,uitableviewdelegate,uitableviewdatasource> @property (Nonatomic,
Strong) Uisearchcontroller *searchcontroller;
@property (nonatomic, strong) UITableView *tableview;
@property (nonatomic, strong) DCSEARCHRESULTTVC *SEARCHRESULTTVC;

@property (nonatomic, strong) Nsarray *dataarr;
    @end @implementation SEARCHVC-(void) viewdidload {[Super viewdidload];

    Self.view.backgroundColor = [Uicolor Whitecolor];
    [Self configuretableview];
[Self configuresearchcontroller]; }-(Nsarray *) createdata {Nsarray *dataarr = @[@ "Aaliyah" @ "Aaron", @ "Abigail", @ "Adam", @ "Addison", @ "Adrian", @ "A" Iden ", @" Alex ", @" Alexa ", @" Alexander ", @" Alexandra ", @" Alexis ", @" Allison ", @" Alyssa ", @" Amelia ", @" Andrea ", @" Andrew ", @ "Angel", @ "Anna", @ "Annabelle", @ "Anthony", @ "Aria", @ "Ariana", @ "Arianna", @ "Ashley", @ "Aubree", @ "Aubrey", @ "Audrey",@ "Austin", @ "Autumn", @ "Ava", @ "Avery", @ "Ayden", @ "Bailey", @ "Bella", @ "Benjamin", @ "Bentley", @ "Blake", @ "Brandon", @ "B" Rayden ", @" Brianna ", @" Brody ", @" Brooklyn ", @" Bryson ", @" Caleb ", @" Cameron ", @" Camila ", @" Carlos ", @" Caroline ", @" Carson " @ "Carter", @ "Charles", @ "Charlotte", @ "Chase", @ "Chloe", @ "Christian", @ "Christopher", @ "Claire", @ "Colton", @ "Connor @ "Cooper", @ "Damian", @ "Daniel", @ "David", @ "Dominic", @ "Dylan", @ "Easton", @ "Eli", @ "Elijah", @ "Elizabeth", @ "Ella", @ "Ellie", @ "Emily", @ "Emma", @ "Ethan", @ "Eva", @ "Evan", @ "Evelyn", @ "Faith", @ "Gabriel", @ "Gabriella", @ "Gavin", @ "Genesis" ", @" Gianna ", @" Grace ", @" Grayson ", @" Hailey ", @" Hannah ", @" Harper ", @" Henry ", @" Hudson ", @" Hunter ", @" Ian ", @" ISAAC ", @" I Sabella ", @" Isaiah ", @" Jace ", @" Jack ", @" Jackson ", @" Jacob ", @" James ", @" Jasmine ", @" Jason ", @" Jaxon ", @" Jayden ", @" Jerem " Iah ", @" Jocelyn ", @" John ", @" Jonathan ", @" Jordan ", @" Jose ", @" Joseph ", @" Joshua ", @" Josiah ", @" Juan ", @" Julia ", @" Julian " , @ "Justin", @ "Katherine", @ "KayDen ", @" Kayla ", @" Kaylee ", @" Kennedy ", @" Kevin ", @" Khloe ", @" Kimberly ", @" Kylie ", @" Landon ", @" Lauren ", @" Layla ", @" Leah " @ "Levi", @ "Liam", @ "Lillian", @ "Lily", @ "Logan", @ "London", @ "Lucas", @ "Lucy", @ "Luis", @ "Luke", @ "Lydia", @ "Mackenzie"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.