objective c - AutoComplete textfield with static array data in IOS -


i have static array of cities names. have text-field , table-view under it. when type city name shows array in table-view correctly, not working when type letter.

for example - if enter k should show city name starting k in table-view.

my code is,

- (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section {      return [city count]; }  - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath {    static nsstring *cellidentifier = @"cell";    uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier];    if (cell == nil) {       cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier];   }   cell.textlabel.text=[city objectatindex:indexpath.row];    return cell; }  - (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath {  uitableviewcell *selectedcell=[tableview cellforrowatindexpath:indexpath]; nslog(@"%@",selectedcell.textlabel.text);     self.cityname.text=[city objectatindex:indexpath.row];     self.autotable.hidden=yes;     } 

and here method on changed on text-field,

- (ibaction)editonchanged:(id)sender {   if(self.cityname.text.length>3)   {      [self getautocompleteplaces:self.cityname.text];   } }  -(void)getautocompleteplaces:(nsstring *)searchtoken {   if(city.count >0)   {     self.autotable.hidden=no;     [_autotable reloaddata];   } } 

for filtering city searched string can try use below code

-(void)getautocompleteplaces:(nsstring *)searchtoken {   //store original city array @ somewhere reset filtering    if(searchtoken.length >0)   {     nsmutablestring *arg = [nsmutablestring string];     [arg appendstring:@"\\s*\\b"];     [arg appendstring:searchtoken];     [arg appendstring:@"\\b\\s*"];     //arg above exact matching string comes start, mid or end     nspredicate *matchpredicate = [nspredicate predicatewithformat:@"self matches[c] %@", arg];      //format can 'self contains[c]' 'self matches[c]' both work different needs      //for 'contains' format no need create arg     nspredicate *containpredicate = [nspredicate predicatewithformat:@"self contains[c] %@", arg];       //get new filtered array old city array     nsarray *newarray = [city filteredarrayusingpredicate:containpredicate]; //can matchpredicate      //use newarray reload table     city = newarray      //reload     self.autotable.hidden=no;     [_autotable reloaddata];   } } 

you can modify predicate format according you


Comments

Popular posts from this blog

neo4j - finding mutual friends in a cypher statement starting with three or more persons -

php - How to remove letter in front of the word laravel -

minify - Minimizing css files -