0

So as of right now I am trying to implement a UISearchBarController and have been successful in filtering and getting it to do the searching right, however when I search whats displayed in the UITableviewCells is incorrect. The app is just a test app and all I want is be able to search through a bunch of different cells. Here's my code and an image as to how the search looks now:

Here's my .h:

#import <UIKit/UIKit.h>

@interface ExerciseViewController : UITableViewController <UITableViewDelegate,   UITableViewDataSource>

@end

And my .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 is my data, what I want to display is the 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 the array together with all the data.
tableData = [NSArray arrayWithObjects:exercise1,exercise2,exercise3,exercise4,exercise5,exercise6,nil];

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//One Section for 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 the problem is 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 is where i give the name but 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 i start to 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 the search looks now:

(obviously I want to display to show the name of the exercise, not whatever its doing now)

enter image description here

Please help, it would be greatly appreciated. Thanks

user2600115
  • 67
  • 1
  • 5

1 Answers1

1

searchResults is an array of Exercises objects, therefore

cell.textLabel.text = [NSString stringWithFormat:@"%@",[searchResults objectAtIndex:indexPath.row]];

just shows the (default) description of such an object. What you probably want is (as in the else-case):

Exercises *exercise = [searchResults objectAtIndex:indexPath.row];
cell.textLabel.text = exercise.name;
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • is there any way that i can add the detail text and the uiimage as well @Martin R – user2600115 Aug 23 '13 at 21:41
  • @user2600115: Have you tried it as in the else-case for the "normal" table view? – Martin R Aug 23 '13 at 21:49
  • sorry whats the else-case? – user2600115 Aug 23 '13 at 21:51
  • In your code you have `if (tableView == self.searchDisplayController.searchResultsTableView) { ... } else { ... }`. The first ("if") case is for the search results table view, and the second ("else") case is for the normal table view. The only difference should be that the first case accesses the `searchResults` array, and the second case accesses the `tableData` array. – Martin R Aug 23 '13 at 21:53
  • ok so i got the image to work by adding that into the searchbar's statement but i also tried this " cell.detailTextLabel.text = [NSString stringWithFormat:@"Type: %@",exercise.status];" which is the detailtextlabel and thats not showing. – user2600115 Aug 23 '13 at 22:16
  • You have to create cells with `UITableViewCellStyleSubtitle` instead of `UITableViewCellStyleDefault` if you want a detail label. – Martin R Aug 23 '13 at 22:20