UISearchBar推送数据问题

时间:2014-05-19 19:16:42

标签: ios objective-c xcode uisearchbar

我已经在我的应用程序中实现了一个搜索栏,并且遇到了我一直试图解决的问题,但似乎无法在任何地方找到解决方案。当您点击搜索栏并键入状态时,您可以选择按该状态,然后将您带到该状态的区域。它确实带你到这些区域,但它每次都会带你到相同的信息....我会在下面显示屏幕截图,也很抱歉这是一个很长的帖子,我只是想确定你们都明白了我在谈论图片和代码。

搜索阿拉巴马州:

enter image description here

阿拉巴马州的结果:

enter image description here

搜索科罗拉多州:

enter image description here

科罗拉多州结果:

enter image description here

正如你所看到的,它每次都给我相同的结果(阿拉巴马州的结果)。我希望它能够给出我所受压迫州的结果(阿拉巴马州 - >阿拉巴马州,科罗拉多州 - >科罗拉多地区)。我知道在我的 prepareForSegue 方法中有一些事情要做,但似乎无法知道究竟要改变什么。我现在将在下面发布我的代码:

RootTableViewController.h:

#import <UIKit/UIKit.h>

@interface RootTableViewController : UITableViewController

@property (nonatomic, strong) NSMutableArray *objects;
@property (nonatomic, strong) NSMutableArray *results;

@property (nonatomic, strong) IBOutlet UISearchBar *searchBar;


@end

RootTableViewController.m

#import "RootTableViewController.h"
#import "SecondTableViewController.h"

@interface RootTableViewController ()

@end

@implementation RootTableViewController
{
NSArray *states;
}

- (NSMutableArray*)objects;
{
if (!_objects)
{
    _objects = [[NSMutableArray alloc] init];
}

return _objects;
}

- (NSMutableArray*)results;
{
if (!_results)
{
    _results = [[NSMutableArray alloc] init];
}

return _results;
}

- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];

states = [NSArray arrayWithObjects:@"Alabama", @"Georgia", @"Tennessee", @"Colorado", nil];

[self.objects addObjectsFromArray:states];
}


- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}


- (void)searchThroughData
{
self.results = nil;

NSPredicate *resultsPredicate = [NSPredicate predicateWithFormat:@"SELF contains [search] %@", self.searchBar.text];

self.results = [[self.objects filteredArrayUsingPredicate:resultsPredicate] mutableCopy];
}

- (void) searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
[self searchThroughData];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.tableView)
{
    return self.objects.count;
}

else
{
    [self searchThroughData];
    return self.results.count;
}
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//table identifier
static NSString *simpleTableIdentifier = @"StateCell";

//creating a cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

if (!cell)
{
    cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:simpleTableIdentifier];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}

if (tableView == self.tableView)
{
    cell.textLabel.text = self.objects[indexPath.row];
}

else
{
    cell.textLabel.text = self.results[indexPath.row];
}

return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (self.searchDisplayController.isActive)
{
    [self performSegueWithIdentifier:@"showStateDetail" sender:self];
}
}

- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];

if (self.searchBar.text.length)
{
    [self.searchDisplayController setActive:NO animated:NO];
}
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"showStateDetail"])
{
    NSString *object = nil;
    NSIndexPath *indexPath = nil;

    if(self.searchDisplayController.isActive)
    {
        indexPath = [[self.searchDisplayController searchResultsTableView] indexPathForSelectedRow];
        object = self.results[indexPath.row];
    }

    else
    {
        indexPath = [self.tableView indexPathForSelectedRow];
        object = self.objects[indexPath.row];
    }

    SecondTableViewController *destViewController = segue.destinationViewController;
    destViewController.stateName = [states objectAtIndex:indexPath.row];

}

}
@end

由于

1 个答案:

答案 0 :(得分:0)

我想出了我的问题,我需要在我的segue底部添加:

destViewController.stateName = object;

而不是:

destViewController.stateName = [states objectAtIndex:indexPath.row];