ios - UISearchbar Controller problems, displaying results? -


so of right trying implement uisearchbarcontroller , have been successful in filtering , getting searching right, when search whats displayed in uitableviewcells incorrect. app test app , want able search through bunch of different cells. here's code , image how search looks now:

here's .h:

#import <uikit/uikit.h>  @interface exerciseviewcontroller : uitableviewcontroller <uitableviewdelegate,   uitableviewdatasource>  @end 

and .m:

#import "exerciseviewcontroller.h" #import "detailviewcontroller.h" #import "exercises.h"  @interface exerciseviewcontroller ()  @end  @implementation exerciseviewcontroller { nsarray *tabledata; nsarray *searchresults; }  @synthesize tableview = _tableview;   - (void)viewdidload { [super viewdidload];  //this data, want display exercise name. exercises *exercise1 = [exercises new]; exercise1.name = @"barbell rollouts"; exercise1.materials = @"30 min"; exercise1.imagefile = @"barbellrollouts.jpg"; exercise1.sets = @"2"; exercise1.reps = @"10"; exercise1.instructions = @"hello"; exercise1.status = @"dynamic";  exercises *exercise2 = [exercises new]; exercise2.name = @"barbell trunk rotation"; exercise2.materials = @"30 min"; exercise2.imagefile = @"barbelltrunkrotation.jpg"; exercise2.sets = @"2"; exercise2.reps = @"10"; exercise2.instructions = @"";   exercise2.status = @"dynamic";  exercises *exercise3 = [exercises new]; exercise3.name = @"bent knee leg raises"; exercise3.materials = @"30 min"; exercise3.imagefile = @"bentkneelegraises.jpg"; exercise3.sets = @"2"; exercise3.reps = @"10"; exercise3.instructions = @"";   exercise3.status = @"dynamic";  exercises *exercise4 = [exercises new]; exercise4.name = @"bicycle manouver"; exercise4.materials = @"30 min"; exercise4.imagefile = @"bicyclemanouver.jpg"; exercise4.sets = @"2"; exercise4.reps = @"10"; exercise4.instructions = @"";   exercise4.status = @"dynamic";  exercises *exercise5 = [exercises new]; exercise5.name = @"boat pose"; exercise5.materials = @"30 min"; exercise5.imagefile = @"boatpose.jpg"; exercise5.sets = @"2"; exercise5.reps = @"10"; exercise5.instructions = @"";   exercise5.status = @"static";  exercises *exercise6 = [exercises new]; exercise6.name = @"bosu boat pose"; exercise6.materials = @"30 min"; exercise6.imagefile = @"bosuboatpose.jpg"; exercise6.sets = @"2"; exercise6.reps = @"10"; exercise6.instructions = @"";   exercise6.status = @"static";  //now putting array data. tabledata = [nsarray arraywithobjects:exercise1,exercise2,exercise3,exercise4,exercise5,exercise6,nil];  - (nsinteger)numberofsectionsintableview:(uitableview *)tableview { //one section these exercises. return 1; }  - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { if (tableview == self.searchdisplaycontroller.searchresultstableview) {     return [searchresults count];  } else {     return [tabledata count];  } }  //i think problem beyond here.  - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath   *)indexpath { static nsstring *simpletableidentifier = @"exercisecell";  uitableviewcell *cell = [self.tableview dequeuereusablecellwithidentifier:simpletableidentifier];  if (cell == nil) {  cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:simpletableidentifier];  }  if (tableview == self.searchdisplaycontroller.searchresultstableview) {    //this give name it's not working.      cell.textlabel.text = [nsstring stringwithformat:@"%@",[searchresults objectatindex:indexpath.row]];  } else {      exercises *exercise = [tabledata objectatindex:indexpath.row];     cell.textlabel.text = exercise.name;     cell.imageview.image = [uiimage imagenamed:exercise.imagefile];     cell.detailtextlabel.text = [nsstring stringwithformat:@"type: %@",exercise.status];  }  cell.detailtextlabel.font = [uifont fontwithname:@"avenir-black" size:16]; cell.textlabel.font = [uifont fontwithname:@"avenir-black" size:20]; cell.textlabel.textcolor = [uicolor peterrivercolor]; cell.detailtextlabel.textcolor = [uicolor peterrivercolor];  return cell; }   //here start filter.  - (void)filtercontentforsearchtext:(nsstring*)searchtext scope:(nsstring*)scope { nspredicate *resultpredicate = [nspredicate predicatewithformat:@"name contains[cd] %@", searchtext];  searchresults = [tabledata filteredarrayusingpredicate:resultpredicate]; }  -(bool)searchdisplaycontroller:(uisearchdisplaycontroller *)controller shouldreloadtableforsearchstring:(nsstring *)searchstring {  [self filtercontentforsearchtext:searchstring                            scope:[[self.searchdisplaycontroller.searchbar     scopebuttontitles]                                   objectatindex:[self.searchdisplaycontroller.searchbar                                                  selectedscopebuttonindex]]];    return yes; } 

how search looks now:

(obviously want display show name of exercise, not whatever doing now)

enter image description here

please help, appreciated. thanks

searchresults array of exercises objects, therefore

cell.textlabel.text = [nsstring stringwithformat:@"%@",[searchresults objectatindex:indexpath.row]]; 

just shows (default) description of such object. want (as in else-case):

exercises *exercise = [searchresults objectatindex:indexpath.row]; cell.textlabel.text = exercise.name; 

Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -