ios - Search for parse user to return multiple results -
i have uitableviewcontroller
, in uisearchbar
searches parse.com users. when search type "abc", want users string "abc" not closets match.
.h
@property (weak, nonatomic) iboutlet uisearchbar *searchbar; @property (nonatomic, strong) pfuser *founduser; @property (nonatomic, strong) pfrelation *friendsrelation; @property (nonatomic, strong) nsarray *allusers; @property (nonatomic, strong) pfuser *currentuser;
.m
- (void)viewdidload { [super viewdidload]; self.searchbar.delegate = self; } -(void)viewdidappear:(bool)animated{ [super viewdidappear:animated]; pfquery *query = [pfuser query]; [query orderbyascending:@"username"]; [query findobjectsinbackgroundwithblock:^(nsarray *objects, nserror *error){ if (error) { nslog(@"error: %@ %@", error, [error userinfo]); } else { self.allusers = objects; [self.tableview reloaddata]; } }]; self.currentuser = [pfuser currentuser]; } #pragma mark -search bar - (void)searchbarcancelbuttonclicked:(uisearchbar *)searchbar { //dismiss keyboard , reload table [self.searchbar resignfirstresponder]; [self.tableview reloaddata]; } - (void)searchbartextdidbeginediting:(uisearchbar *)searchbar { //enable cancel button when user touches search field self.searchbar.showscancelbutton = true; } - (void)searchbartextdidendediting:(uisearchbar *)searchbar { //disable cancel button when user ends editing self.searchbar.showscancelbutton = false; } - (void)searchbarsearchbuttonclicked:(uisearchbar *)searchbar { //dismiss keyboard [self.searchbar resignfirstresponder]; //reset founduser property self.founduser = nil; //strip whitespace off end of search text nsstring *searchtext = [self.searchbar.text stringbytrimmingcharactersinset:[nscharacterset whitespaceandnewlinecharacterset]]; searchtext = [searchtext lowercasestring]; //check make sure field isnt empty , query parse username in text field if (![searchtext isequaltostring:@""]) { pfquery *query = [pfuser query]; [query wherekey:@"username" containsstring:searchtext]; [query findobjectsinbackgroundwithblock:^(nsarray *objects, nserror *error) { if (!error) { //check make sure query found user if (objects.count > 0) { //set founduser property user found query (we use last object since array) self.founduser = objects.lastobject; //the query succesful returned no results. user not found, display error message } else { } //reload tableview after user searches [self.tableview reloaddata]; } else { //error occurred query } }]; } } #pragma mark - table view data source - (nsinteger)numberofsectionsintableview:(uitableview *)tableview { // return number of sections. if (self.founduser) { return 1; } else { return 0; } } - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { // return number of rows in section. if (self.founduser) { return 1; } else { return 0; } }
how can search string , retrieve users string?
Comments
Post a Comment