ios - search in grouped uitableview -


i new in objectiv-c developpement , , try filter content of grouped tableview.this did still dosen't work :

- (void)filtercontentforsearchtext:(nsstring *)searchtext scope:(nsstring *)scope {      nspredicate *resultpredicate = [nspredicate predicatewithformat:@"self contains[cd] %@", searchtext];;       //activities table contains objects display contents in table     self.filtereddata = [activities filteredarrayusingpredicate:resultpredicate]; }  - (bool)searchdisplaycontroller:(uisearchdisplaycontroller *)controller shouldreloadtableforsearchscope:(nsinteger)searchoption {     [self filtercontentforsearchtext:[self.searchdisplaycontroller.searchbar text] scope:      [[self.searchdisplaycontroller.searchbar scopebuttontitles]       objectatindex:searchoption]];      // return yes cause search result table view reloaded.     return yes; } //activite object use desplay attribut in table ipadactivity *activite ;     if (thetableview == self.searchdisplaycontroller.searchresultstableview) {         activite = [self.filtereddata objectatindex:indexpath.row];       }      else{         activite = [[objects                       objectforkey:[objectsindex objectatindex:indexpath.section]] objectatindex:indexpath.row];     } 

any suggestion please

// //  viewcontroller.m //  searchtut //   #import "viewcontroller.h" #import "searchtablecell.h" #import "xmlreader.h"  @interface viewcontroller ()<uitableviewdatasource, uitableviewdelegate, uisearchbardelegate>  @property (weak, nonatomic) iboutlet uitableview *tablview; @property (weak, nonatomic) iboutlet uisearchbar *searchbr;  @property (nonatomic, strong) nsmutablearray *arraylist; //main table array @property (nonatomic, strong) nsmutablearray *arrayfilteredlist; //array search results while user types in search bar @property (nonatomic, strong) nsmutabledictionary *dictlist; //need manufacture equivalent dictionary below json array /*  @{      @"b" : @[@"bear", @"black swan", @"buffalo"],      @"c" : @[@"camel", @"cockatoo"],      @"d" : @[@"dog", @"donkey"],      @"e" : @[@"emu"],      @"g" : @[@"giraffe", @"greater rhea"],      @"h" : @[@"hippopotamus", @"horse"],      @"k" : @[@"koala"],      @"l" : @[@"lion", @"llama"],      @"m" : @[@"manatus", @"meerkat"],      @"p" : @[@"panda", @"peacock", @"pig", @"platypus", @"polar bear"],      @"r" : @[@"rhinoceros"],      @"s" : @[@"seagull"],      @"t" : @[@"tasmania devil"],      @"w" : @[@"whale", @"whale shark", @"wombat"]  };  refer: https://www.appcoda.com/ios-programming-index-list-uitableview/ */  @property (nonatomic, strong) nsmutabledictionary *dictlistfiltered; //dictionary when searched @property (nonatomic, assign) bool isfiltered; //if user starts entering text in search bar, flag yes. if there no text in search bar, flag no.  @end  @implementation viewcontroller  - (void)viewdidload {     [super viewdidload];     // initialisers     self.tablview.datasource = self;     self.tablview.delegate = self;     self.searchbr.delegate = self;     self.arraylist = [nsmutablearray array];     self.dictlist = [nsmutabledictionary dictionary];     self.dictlistfiltered = [nsmutabledictionary dictionary];     self.isfiltered = no;      //get , parse xml file , load result in array     nsstring *strxmlpath = [[nsbundle mainbundle] pathforresource:@"list" oftype:@"xml"];     nserror *error;     nsstring *strxml = [nsstring stringwithcontentsoffile:strxmlpath encoding:nsutf8stringencoding error:&error];     nsdictionary *dict = [xmlreader dictionaryforxmlstring:strxml error:&error];     nsdictionary *dictpricelist = [dict objectforkey:@"pricelist"];     nsarray *arrayline = [dictpricelist objectforkey:@"line"];     nsmutablearray *arraytemppricelist = [nsmutablearray array];     (nsdictionary *dict in arrayline) {         [arraytemppricelist addobject:dict];     }     self.arraylist = arraytemppricelist;     nslog(@"%@", self.arraylist);      //create dictionary sectioned table grouped respect pricelistcategory value. pricelistcategory value set in section title. logic group table in sections below     [self groupxmlinsectionswitharray:arraytemppricelist]; }  - (void)groupxmlinsectionswitharray:(nsmutablearray *)arrayjson {     if (arrayjson.count > 0) { //added condition reset json during search.         (nsdictionary *dict in arrayjson ) {             nsstring *strpricelistcategory = [[dict objectforkey:@"pricelistcategory"] objectforkey:@"text"];             if (self.isfiltered) {                 if ([[self.dictlistfiltered allkeys] containsobject:strpricelistcategory]) {                     nsmutablearray *arraytemp = [self.dictlistfiltered objectforkey:strpricelistcategory];                     [arraytemp addobject:dict];                     [self.dictlistfiltered setobject:arraytemp forkey:strpricelistcategory];                 } else {                     nsmutablearray *arraytemp = [[nsmutablearray alloc] initwithobjects:dict, nil];                     [self.dictlistfiltered setobject:arraytemp forkey:strpricelistcategory];                 }             } else {                 if ([[self.dictlist allkeys] containsobject:strpricelistcategory]) {                     nsmutablearray *arraytemp = [self.dictlist objectforkey:strpricelistcategory];                     [arraytemp addobject:dict];                     [self.dictlist setobject:arraytemp forkey:strpricelistcategory];                 } else {                     nsmutablearray *arraytemp = [[nsmutablearray alloc] initwithobjects:dict, nil];                     [self.dictlist setobject:arraytemp forkey:strpricelistcategory];                 }             }          }     } else { //if search results yield no json array, remove objects dictionary         [self.dictlist removeallobjects];         [self.dictlistfiltered removeallobjects];     }     [self.tablview reloaddata]; }  - (void)didreceivememorywarning {     [super didreceivememorywarning];     // dispose of resources can recreated. }  #pragma mark - tableview datasource  - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section {     nsarray *arraytemp = [nsarray array];     if (self.isfiltered) {         nsarray *arraylistallkeys = [self.dictlistfiltered allkeys];         arraytemp = [self.dictlistfiltered objectforkey:[arraylistallkeys objectatindex:section]];     } else {         nsarray *arraylistallkeys = [self.dictlist allkeys];         arraytemp = [self.dictlist objectforkey:[arraylistallkeys objectatindex:section]];     }     return [arraytemp count]; }  - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath {     searchtablecell *cell = [tableview dequeuereusablecellwithidentifier:@"searchtablecellid" forindexpath:indexpath];     nsdictionary *dict;     if (self.isfiltered) {         nsarray *arraypricelistallkeys = [self.dictlistfiltered allkeys];         nsarray *arrayprice = [self.dictlistfiltered objectforkey:[arraypricelistallkeys objectatindex:indexpath.section]];         dict = [arrayprice objectatindex:indexpath.row];     } else {         nsarray *arraypricelistallkeys = [self.dictlist allkeys];         nsarray *arrayprice = [self.dictlist objectforkey:[arraypricelistallkeys objectatindex:indexpath.section]];         dict = [arrayprice objectatindex:indexpath.row];     }     cell.lblbarcode.text = [[dict objectforkey:@"apnbarcode"] objectforkey:@"text"];     cell.lblpackdescription.text = [[dict objectforkey:@"packdescription"] objectforkey:@"text"];     cell.lblproduct.text = [[dict objectforkey:@"product"] objectforkey:@"text"];     cell.lblproductname.text = [[dict objectforkey:@"productname"] objectforkey:@"text"];      return cell; }  - (nsinteger)numberofsectionsintableview:(uitableview *)tableview {     if (self.isfiltered) {         return [[self.dictlistfiltered allkeys] count];     } else {         return [[self.dictlist allkeys] count];     } }  - (nsstring *)tableview:(uitableview *)tableview titleforheaderinsection:(nsinteger)section {     nsstring *price = @"";     if (self.isfiltered) {         price = [[self.dictlistfiltered allkeys] objectatindex:section];     } else {         price = [[self.dictlist allkeys] objectatindex:section];     }     return price; }  - (cgfloat) tableview:(uitableview *)tableview heightforheaderinsection:(nsinteger)section {     return 50.0; }  - (uiview *)tableview:(uitableview *)tableview viewforheaderinsection:(nsinteger)section {     nsstring *sectiontitle = [self tableview:tableview titleforheaderinsection:section];     uilabel *label = [[uilabel alloc] initwithframe:cgrectmake(8, 8, self.tablview.frame.size.width - 16, 30)];     //if add bit x , decrease y, more in line tableview cell (that in ipad , landscape)     label.backgroundcolor = [uicolor clearcolor];     label.textcolor = [uicolor whitecolor];     label.shadowcolor = [uicolor whitecolor];     label.shadowoffset = cgsizemake(0.5, 0.5);     label.font = [uifont boldsystemfontofsize:18];     label.text = sectiontitle;      // create header view , add label subview     uiview *viewheader = [[uiview alloc] initwithframe:cgrectmake(0, 0, self.tablview.frame.size.width, 50)];     viewheader.backgroundcolor = [uicolor colorwithred:0.4 green:0.4 blue:0.4 alpha:1];     [viewheader addsubview:label];     return viewheader; }  #pragma mark - searchbar delegates  - (void)updatesearchresults {     //you can use below commented code in case need exact match of product value text entered in search bar     /*      nsmutablearray *searchresults = [self.arraylist mutablecopy];      nsstring *strippedstring = [searchtext stringbytrimmingcharactersinset:[nscharacterset whitespacecharacterset]];      nspredicate *predicate = [nspredicate predicatewithformat:@"product.text==[c] %@", strippedstring];      searchresults = [[searchresults filteredarrayusingpredicate:predicate] mutablecopy];      // hand on filtered results our search results table      self.arrayfilteredlist = searchresults;      //nslog(@"%@", self.arrayfilteredpricelist);      [self groupxmlinsectionswitharray:self.arrayfilteredlist];      */       //below code searches depending on product / product name / apnbarcode values     nsstring *searchtext = self.searchbr.text;     nsmutablearray *searchresults = [self.arraylist mutablecopy];     nsstring *strippedstring = [searchtext stringbytrimmingcharactersinset:[nscharacterset whitespacecharacterset]];     nsarray *searchitems = nil;     if (strippedstring.length > 0) {         searchitems = [strippedstring componentsseparatedbystring:@" "];     }      nsmutablearray *andmatchpredicates = [nsmutablearray array];      (nsstring *searchstring in searchitems) {         nsmutablearray *searchitemspredicate = [nsmutablearray array];          // below use nsexpression represent expressions in our predicates.         // nspredicate made of smaller, atomic parts: 2 nsexpressions (a left-hand value , right-hand value)          // product         nsexpression *lhsproduct = [nsexpression expressionforkeypath:@"product.text"];         nsexpression *rhsproduct = [nsexpression expressionforconstantvalue:searchstring];         nspredicate *finalpredicateproduct = [nscomparisonpredicate                                               predicatewithleftexpression:lhsproduct                                               rightexpression:rhsproduct                                               modifier:nsdirectpredicatemodifier                                               type:nscontainspredicateoperatortype                                               options:nscaseinsensitivepredicateoption];         [searchitemspredicate addobject:finalpredicateproduct];          // product name         nsexpression *lhsproductname = [nsexpression expressionforkeypath:@"productname.text"];         nsexpression *rhsproductname = [nsexpression expressionforconstantvalue:searchstring];         nspredicate *finalpredicateproductname = [nscomparisonpredicate                                                   predicatewithleftexpression:lhsproductname                                                   rightexpression:rhsproductname                                                   modifier:nsdirectpredicatemodifier                                                   type:nscontainspredicateoperatortype                                                   options:nscaseinsensitivepredicateoption];         [searchitemspredicate addobject:finalpredicateproductname];           // apn bar code         nsexpression *lhsapnbarcode = [nsexpression expressionforkeypath:@"apnbarcode.text"];         nsexpression *rhsapnbarcode = [nsexpression expressionforconstantvalue:searchstring];         nspredicate *finalpredicateapnbarcode= [nscomparisonpredicate                                                 predicatewithleftexpression:lhsapnbarcode                                                 rightexpression:rhsapnbarcode                                                 modifier:nsdirectpredicatemodifier                                                 type:nscontainspredicateoperatortype                                                 options:nscaseinsensitivepredicateoption];         [searchitemspredicate addobject:finalpredicateapnbarcode];          // averagecost         nsexpression *lhspackdescription = [nsexpression expressionforkeypath:@"packdescription.text"];         nsexpression *rhspackdescription = [nsexpression expressionforconstantvalue:searchstring];         nspredicate *finalpredicatepackdescriptiont = [nscomparisonpredicate                                                 predicatewithleftexpression:lhspackdescription                                                 rightexpression:rhspackdescription                                                 modifier:nsdirectpredicatemodifier                                                 type:nscontainspredicateoperatortype                                                 options:nscaseinsensitivepredicateoption];         [searchitemspredicate addobject:finalpredicatepackdescriptiont];          // @ or predicate our master , predicate         nscompoundpredicate *ormatchpredicates = [nscompoundpredicate orpredicatewithsubpredicates:searchitemspredicate];         [andmatchpredicates addobject:ormatchpredicates];      }      // match fields of product object     nscompoundpredicate *finalcompoundpredicate =     [nscompoundpredicate andpredicatewithsubpredicates:andmatchpredicates];     searchresults = [[searchresults filteredarrayusingpredicate:finalcompoundpredicate] mutablecopy];     // hand on filtered results our search results table     self.arrayfilteredlist = searchresults;     //nslog(@"%@", self.arrayfilteredpricelist);      [self groupxmlinsectionswitharray:self.arrayfilteredlist]; }  //this method gets called when user starts entering text in search bar - (void)searchbar:(uisearchbar *)searchbar textdidchange:(nsstring *)searchtext {     if (searchtext.length == 0) {         self.isfiltered = no;         [self.dictlist removeallobjects];         [self groupxmlinsectionswitharray:self.arraylist];         [self.tablview reloaddata];     } else {         self.isfiltered = yes;         self.arrayfilteredlist = [nsmutablearray new];         [self.dictlistfiltered removeallobjects];         [self updatesearchresults];     } }  - (void)searchbarcancelbuttonclicked:(uisearchbar *)searchbar {     [searchbar resignfirstresponder];     [self.dictlist removeallobjects];     [self groupxmlinsectionswitharray:self.arraylist];     [self.tablview reloaddata]; }  - (void)searchbarsearchbuttonclicked:(uisearchbar *)searchbar {     [searchbar resignfirstresponder];     [self.tablview reloaddata]; }   @end 

above code logic based on json dictionaries contained in array , create sections depending on pricelistcategory value

 [     {         "product": {           "text": "geldcmframe"        },        "productmajorgroup": {           "text": "ic"        },        "pricelistcategory": {           "text": "geldisp"        },        "apnbarcode": {           "text": "931000"        },        "packdescription": {           "text": "some description"        },        "productname": {           "text": "description"        }        , on     }, , on  ] 

refer screenshot below search results enter image description here


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

qt - Errors in generated MOC files for QT5 from cmake -