Fuzzy Search Uisearchbar

Source: Internet
Author: User

#import "Search_viewcontroller.h"

@interface Search_viewcontroller () <uitableviewdatasource,uitableviewdelegate,uisearchdisplaydelegate, Uisearchbardelegate>

@property (nonatomic,strong) nsarray*dataarr;//data source

@property (Nonatomic,strong) nsarray*resultsarr;//search results

@property (Nonatomic,strong) Uisearchbar*search;

@property (Nonatomic,strong) uisearchdisplaycontroller* searchplay;

@property (Nonatomic,strong) Uitableview*atableview;

@end

@implementation Search_viewcontroller

-(void) viewdidload

{

[Super Viewdidload];

[Email protected] "search";

_dataarr= [[Nsarray alloc]initwithobjects:@ "AAA", @ "abc", @ "Aqq", @ "BDC", @ "GCD", @ "MNB", @ "zzz", nil];

[Self createview];

Do any additional setup after loading the view.

}

-(void) CreateView

{

_atableview= [[UITableView Alloc]initwithframe:cgrectmake (0,0,320,480)];

_atableview.delegate=self;

_atableview.datasource=self;

[Self.view Addsubview:_atableview];

}

#pragma mark-

#pragma Mark Uitableviewdelegate

-(uiview*) TableView: (uitableview*) TableView viewforheaderinsection: (nsinteger) Section

{

_search= [[Uisearchbar Alloc]initwithframe:cgrectmake (0,0,320,40)];

_search.backgroundcolor= [Uicolor Redcolor];

_search.delegate=self;

_search.showscancelbutton=yes;

_search.keyboardtype=uikeyboardtypedefault;

_searchplay= [[Uisearchdisplaycontroller Alloc]initwithsearchbar:self.search contentscontroller:self];

_searchplay.delegate=self;

_searchplay.searchresultsdatasource=self;

_searchplay.searchresultsdelegate=self;

_searchplay.active=no;

return _searchplay.searchbar;

}

-(BOOL) searchbarshouldendediting: (uisearchbar*) Searchbar

{

NSLog (@ "Cancel");

return YES;

}

-(CGFloat) TableView: (uitableview*) TableView heightforheaderinsection: (nsinteger) Section

{

return TableView ==self.searchplay.searchresultstableview?0:40;

}

-(Nsinteger) TableView: (uitableview*) TableView numberofrowsinsection: (Nsinteger) section{

Nsinteger row = 0;

if ([TableView IsEqual:self.searchPlay.searchResultsTableView]) {

row = [Self.resultsarr count];

}else{

row = [Self.dataarr count];

}

return row;

}

-(uitableviewcell*) TableView: (uitableview*) TableView Cellforrowatindexpath: (nsindexpath*) Indexpath

{

static NSString *cellidentifier [email protected] "Cell";

Uitableviewcell*cell = [TableView dequeuereusablecellwithidentifier:cellidentifier];

if (!cell) {

cell = [[UITableViewCell alloc]initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier];

}

if ([TableView IsEqual:self.searchPlay.searchResultsTableView]) {

cell.textlabel.text= [Self.resultsarr ObjectAtIndex:indexPath.row];

}else{

cell.textlabel.text= [Self.dataarr ObjectAtIndex:indexPath.row];

}

return cell;

}

#pragma mark-

#pragma Mark Uisearchdisplaycontrollerdelegate

Text is the input literal, scope is the search scope

-(void) SearchText: (nsstring*) Text andwithscope: (nsstring*) scope

{

Contains is a string comparison operator,

Nspredicate*result = [Nspredicate predicatewithformat:@ "self contains[cd]%@", text];

self.resultsarr= [Self.dataarr Filteredarrayusingpredicate:result];

}

-(BOOL) Searchdisplaycontroller: (uisearchdisplaycontroller*) controller shouldreloadtableforsearchstring: ( nsstring*) searchstring

{

SearchString is the text that is entered

Return result data read search scope in selection

Nsarray*searscope = [Self.searchPlay.searchBar scopebuttontitles];//array Range

[Self searchtext:searchstring andwithscope:[searscope Objectatindex:[self.searchplay.searchbar Selectedscopebuttonindex]];

return YES;

}

-(BOOL) Searchdisplaycontroller: (uisearchdisplaycontroller*) controller shouldreloadtableforsearchscope: ( Nsinteger) searchoption

{

Nsstring*inputtext =self.searchplay.searchbar.text;//Search for input text

Nsarray*searscope = [Self.searchPlay.searchBar scopebuttontitles];//Soso Range

[Self searchtext:inputtext andwithscope:[searscope objectatindex:searchoption];

return YES;

}

Fuzzy Search Uisearchbar

Related Article

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.