Reputation: 1032
I am trying to implement a search bar (UISearchBar) in iPhone. Basically it searches for company names against an API results. So say, if the user enters ABC, immediately this should display ABC 1, ABC 2, ABC 3...
These results ABC 1, ABC 2, ABC 3... actually come from an API call (http://xyz.com/?companyname=ABC)
It should update as the user eneters..i.e. if he now enters ABC 2, it should only display
ABC 2 and not others.
I also want to store the value selected by the user (from those displayed to him)
Can someone provide me references to any example, which searches against an API.
Upvotes: 2
Views: 9660
Reputation: 16553
I will provide a logic for you. I would suggest that you should keep all the ABC vales in a array. For each key input in search bar this delegate will be called
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
NSString *firstLetter = @"";
NSInteger strlen=[searchText length];
for (NSString *ABCValue in ABCArray)
{
if(ABCValue.length >= stringlen)
firstLetter = [buildName substringToIndex:stringlen];
if([searchText.uppercaseString isEqualToString:firstLetter.uppercaseString])
{
[tableArray addObject:ABCValue]; //Store it in NSMutableArray
break;
}
}
[tableView reloadData];
}
In didSelectRowAtIndexPath when user selects a cell with your ABC results the indexPath.row value and the tableArray index value is same. Bingo you get the selected value.
Upvotes: 4
Reputation: 2742
Quote: I also want to store the value selected by the user (from those displayed to him)
For this part i recommend using NSUserDefaults
And for using API's you might want to take a look at this example
Good luck..
Upvotes: 0