UISearchbar控制器问题,显示结果?

时间:2013-08-23 21:15:24

标签: ios uitableview uisearchbar nspredicate uisearchdisplaycontroller

所以到目前为止我正在尝试实现一个UISearchBarController,并且已成功过滤并使其正确进行搜索,但是当我搜索UITableviewCells中显示的内容不正确时。该应用程序只是一个测试应用程序,我想要的只是能够搜索一堆不同的单元格。这是我的代码和关于搜索现在如何看的图像:

这是我的.h:

#import <UIKit/UIKit.h>

@interface ExerciseViewController : UITableViewController <UITableViewDelegate,   UITableViewDataSource>

@end

我的.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;
}

现在搜索的内容如下:

(显然我希望显示以显示练习的名称,而不是现在正在做什么)

enter image description here

请帮助,我们将不胜感激。 感谢

1 个答案:

答案 0 :(得分:1)

searchResultsExercises个对象的数组,因此

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

只显示此类对象的(默认)描述。你可能想要的是什么 (如在其他情况下):

Exercises *exercise = [searchResults objectAtIndex:indexPath.row];
cell.textLabel.text = exercise.name;