objective c - How can I make my app search an existing UITableView for values selected in a UIPickerView? -
so i've developed app has populated uitableview. uitableview populated data mysql database. uitableview contains working search bar.
however add this, want add 3 column uipickerview mix (already coded).
the idea: users select 3 values (one each column), , hit "go" button. once they've selected values , pressed go, want app filter through uitableview, , show results contain values selected in pickerview.
everything coded, i'm not sure how connect two. how go doing this? i've pasted our code far below (apologies lengthy post, seems valid in case). appreciated.
viewcontroller.h (tableview controller)
#import <uikit/uikit.h> @interface viewcontroller : uiviewcontroller { iboutlet uitableview *straintableview; nsarray *strains; nsarray *searchresults; nsmutabledata *data; } @property (nonatomic, retain) nsarray *searchresults; @end viewcontroller.m (tableview controller)
- (int)numberofsectionsintableview: (uitableview *)tableview { return 1; } - (int)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { if (tableview == self.searchdisplaycontroller.searchresultstableview) { return [searchresults count]; } else { return [strains count]; } } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *straintableidentifier = @"straintablecell"; straintablecell *cell = (straintablecell *)[tableview dequeuereusablecellwithidentifier:straintableidentifier]; if (cell == nil) cell = [[straintablecell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:straintableidentifier]; nsarray *nib = [[nsbundle mainbundle] loadnibnamed:@"straintablecell" owner:self options:nil]; cell = [nib objectatindex:0]; if (tableview == self.searchdisplaycontroller.searchresultstableview) { cell.titlelabel.text = [[searchresults objectatindex:indexpath.row] objectforkey:@"title"]; cell.descriptionlabel.text = [[searchresults objectatindex:indexpath.row] objectforkey:@"description"]; cell.ratinglabel.text = [[searchresults objectatindex:indexpath.row] objectforkey:@"rating"]; nslog(@"%@", searchresults); } else { cell.titlelabel.text = [[strains objectatindex:indexpath.row] objectforkey:@"title"]; cell.descriptionlabel.text = [[strains objectatindex:indexpath.row] objectforkey:@"description"]; cell.ratinglabel.text = [[strains objectatindex:indexpath.row] objectforkey:@"rating"]; } { } return cell; } - (void)filtercontentforsearchtext:(nsstring*)searchtext scope:(nsstring*)scope { nspredicate *resultpredicate = [nspredicate predicatewithformat:@"self contains[cd] %@", searchtext]; searchresults = [strains filteredarrayusingpredicate:resultpredicate]; } -(bool)searchdisplaycontroller:(uisearchdisplaycontroller *)controller shouldreloadtableforsearchstring:(nsstring *)searchstring { [self filtercontentforsearchtext:searchstring scope:[[self.searchdisplaycontroller.searchbar scopebuttontitles] objectatindex:[self.searchdisplaycontroller.searchbar selectedscopebuttonindex]]]; return yes; } - (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath { straindetailviewcontroller *detailviewcontroller = [[straindetailviewcontroller alloc] initwithnibname:@"straindetailviewcontroller" bundle:nil]; if ([searchresults count]) { detailviewcontroller.title = [[searchresults objectatindex:indexpath.row] objectforkey:@"title"]; detailviewcontroller.straindetail = [searchresults objectatindex:indexpath.row]; } else { detailviewcontroller.title = [[strains objectatindex:indexpath.row] objectforkey:@"title"]; detailviewcontroller.straindetail = [strains objectatindex:indexpath.row]; nslog(@"%@", strains); } [self.navigationcontroller pushviewcontroller:detailviewcontroller animated:yes]; } pickerviewcontroller.h
@interface pickerviewcontroller : uiviewcontroller { uipickerview *pickerview; nsmutablearray *array1; nsmutablearray *array2; nsmutablearray *array3; nsarray *strains; nsarray *searchresults; nsmutabledata *data; } - (ibaction)buttonpressed:(uibutton *)sender; @property (nonatomic, retain) iboutlet uipickerview *pickerview; - (void)populatearray1; - (void)populatearray2; - (void)populatearray3; @end **pickerviewcontroller.m** #pragma mark - #pragma mark picker view methods - (nsinteger)numberofcomponentsinpickerview:(uipickerview *)pickerview; { return 3; } - (void)pickerview:(uipickerview *)pickerview didselectrow:(nsinteger)row incomponent:(nsinteger)component { if (component == 0) { nslog(@"you selected %@", [array1 objectatindex:row]); } if (component == 1) { nslog(@"you selected %@", [array2 objectatindex:row]); } if (component == 2) { nslog(@"you selected %@", [array3 objectatindex:row]); } } - (nsinteger)pickerview:(uipickerview *)pickerview numberofrowsincomponent:(nsinteger)component; { if (component == 0) { return [array1 count]; } if (component == 1) { return [array2 count]; } if (component == 2) { return [array3 count]; } else { return [array1 count]; } } - (nsstring *)pickerview:(uipickerview *)pickerview titleforrow:(nsinteger)row forcomponent:(nsinteger)component; { if (component == 0) { return [array1 objectatindex:row]; } if (component == 1) { return [array2 objectatindex:row]; } if (component == 2) { return [array3 objectatindex:row]; } else { return [array2 objectatindex:row]; } } - (void)populatearray1 { array1 = [[nsmutablearray alloc] init]; [array1 addobject:@"arthritis"]; [array1 addobject:@"cancer"]; [array1 addobject:@"hiv"]; [array1 addobject:@"migraines"]; [array1 addobject:@"insomnia"]; } - (void)populatearray2 { array2 = [[nsmutablearray alloc] init]; [array2 addobject:@"nausea"]; [array2 addobject:@"pain"]; [array2 addobject:@"appetite"]; [array2 addobject:@"fever"]; [array2 addobject:@"exhaustion"]; } - (void)populatearray3 { array3 = [[nsmutablearray alloc] init]; [array3 addobject:@"oil"]; [array3 addobject:@"plant"]; [array3 addobject:@"edible"]; [array3 addobject:@"powder"]; } - (ibaction)buttonpressed:(uibutton *)sender { nslog(@"button pushed!"); nspredicate *titlepredicate = [nspredicate predicatewithformat:@"title contains[cd] %@", [pickerview selectedrowincomponent:0]]; nspredicate *descriptionpredicate = [nspredicate predicatewithformat:@"description contains[cd] %@", [pickerview selectedrowincomponent:1]]; nspredicate *ratingpredicate = [nspredicate predicatewithformat:@"rating contains[cd] %@", [pickerview selectedrowincomponent:2]]; nscompoundpredicate *resultpredicate = [nscompoundpredicate andpredicatewithsubpredicates:[nsarray arraywithobjects: titlepredicate,descriptionpredicate,ratingpredicate, nil]]; filterresults = [strains filteredarrayusingpredicate:resultpredicate]; } @end
you can use compound predicate filter on multiple values
nspredicate *titlepredicate = [nspredicate predicatewithformat:@"title contains[cd] %@", tittletex];
nspredicate *descriptionpredicate = [nspredicate predicatewithformat:@"description contains[cd] %@", descriptiontext];
nspredicate *ratingpredicate = [nspredicate predicatewithformat:@"rating contains[cd] %@", ratingtext];
nscompoundpredicate *resultpredicate = [nscompoundpredicate andpredicatewithsubpredicates:[nsarray arraywithobjects: titlepredicate,descriptionpredicate,ratingpredicate];
searchresults = [strains filteredarrayusingpredicate:resultpredicate];
Comments
Post a Comment