#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