0

因此,截至目前,我正在尝试实现 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;
}

搜索现在的样子:

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

在此处输入图像描述

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

4

1 回答 1

1

searchResults是一个Exercises对象数组,因此

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

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

Exercises *exercise = [searchResults objectAtIndex:indexPath.row];
cell.textLabel.text = exercise.name;
于 2013-08-23T21:22:54.280 回答