开发者

UITableView Search Rows Not Corresponding

Right now, I am developing an application which is composed of a table view, with a search bar. When someone taps on one of the rows on the table, it will load a new view that has the corresponding page. This is my code:

RootViewController.h:

@interface RootViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, UISearchDisplayDelegate, UISearchBarDelegate>
{
    UITableView *mainTableView;
    NSMutableArray *contentsList;
    NSMutableArray *searchResults;
    NSString *savedSearchTerm;
}

@property (nonatomic, retain) IBOutlet UITableView *mainTableView;
@property (nonatomic, retain) NSMutableArray *contentsList;
@property (nonatomic, retain) NSMutableArray *searchResults;
@property (nonatomic, copy) NSString *savedSearchTerm;

- (void)handleSearchForTerm:(NSString *)searchTerm;

@end

RootViewController.m:

#import "RootViewController.h"
#import "Hydrogen.h"
#import "Helium.h"

@implementation RootViewController

@synthesize mainTableView;
@synthesize contentsList;
@synthesize searchResults;
@synthesize savedSearchTerm;

- (void)dealloc
{
    [mainTableView release], mainTableView = nil;
    [contentsList release], contentsList = nil;
    [searchResults release], searchResults = nil;
    [savedSearchTerm release], savedSearchTerm = nil;

    [super dealloc];
}

- (void)viewDidUnload
{
    [super viewD开发者_JS百科idUnload];

    // Save the state of the search UI so that it can be restored if the view is re-created.
    [self setSavedSearchTerm:[[[self searchDisplayController] searchBar] text]];

    [self setSearchResults:nil];
}

- (void)viewDidLoad
{

    [super viewDidLoad];

    NSMutableArray *array = [[NSMutableArray alloc] init];
    [array addObject:@"Hydrogen"];
    [array addObject:@"Helium"];    
    [self setContentsList:array];
    [array release], array = nil;

    // Restore search term
    if ([self savedSearchTerm])
    {
        [[[self searchDisplayController] searchBar] setText:[self savedSearchTerm]];
    }
}

- (void)viewWillAppear:(BOOL)animated
{

    [super viewWillAppear:animated];

    [[self mainTableView] reloadData];
}

/*
 // Override to allow orientations other than the default portrait orientation.
 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 // Return YES for supported orientations
 return (interfaceOrientation == UIInterfaceOrientationPortrait);
 }
 */

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)handleSearchForTerm:(NSString *)searchTerm
{

    [self setSavedSearchTerm:searchTerm];

    if ([self searchResults] == nil)
    {
        NSMutableArray *array = [[NSMutableArray alloc] init];
        [self setSearchResults:array];
        [array release], array = nil;
    }

    [[self searchResults] removeAllObjects];

    if ([[self savedSearchTerm] length] != 0)
    {
        for (NSString *currentString in [self contentsList])
        {
            if ([currentString rangeOfString:searchTerm options:NSCaseInsensitiveSearch].location != NSNotFound)
            {
                [[self searchResults] addObject:currentString];
            }
        }
    }
}

#pragma mark -
#pragma mark UITableViewDataSource Methods

- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section
{

    NSInteger rows;

    if (tableView == [[self searchDisplayController] searchResultsTableView])
        rows = [[self searchResults] count];
    else
        rows = [[self contentsList] count];

    return rows;
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{   
    NSInteger row = [indexPath row];
    NSString *contentForThisRow = nil;

    if (tableView == [[self searchDisplayController] searchResultsTableView])
        contentForThisRow = [[self searchResults] objectAtIndex:row];
    else
        contentForThisRow = [[self contentsList] objectAtIndex:row];

    static NSString *CellIdentifier = @"CellIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        // Do anything that should be the same on EACH cell here.  Fonts, colors, etc.
    }

    // Do anything that COULD be different on each cell here.  Text, images, etc.
    [[cell textLabel] setText:contentForThisRow];

    return cell;
}

#pragma mark -
#pragma mark UITableViewDelegate Methods

- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if ([[contentsList objectAtIndex:indexPath.row] isEqual:@"Hydrogen"])
    {
        Hydrogen *hydrogen = [[Hydrogen alloc] initWithNibName:@"Hydrogen" bundle:nil];
        [self.navigationController pushViewController:hydrogen animated:YES];
        [hydrogen release];
    }
    else if ([[contentsList objectAtIndex:indexPath.row] isEqual:@"Helium"])
    {
        Helium *helium = [[Helium alloc] initWithNibName:@"Helium" bundle:nil];
        [self.navigationController pushViewController:helium animated:YES];
        [helium release];
    }
}

#pragma mark -
#pragma mark UISearchDisplayController Delegate Methods

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller 
shouldReloadTableForSearchString:(NSString *)searchString
{
    [self handleSearchForTerm:searchString];
    return YES;
}

- (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller
{
    [self setSavedSearchTerm:nil];

    [[self mainTableView] reloadData];
}

@end

This code all works, perfectly, except for one flaw. When I use the search, the results appear as expected, with results matching the string that was typed in. However, when I tap on one of the rows of the search results, the corresponding XIB file does not load; instead, it loads the XIB file that corresponded with the original text in the UITableView without the search.

For example, if I were to type in the word "Helium" in the search, it would display one result saying "Helium," but when I tap into the result it loads the "Hydrogen" page, because hydrogen was the original link that was accessed when the first row was tapped.

Can anyone help me with this? I have spent many days on this code, and it is really getting frustrating.

Thank anyone out there who can help me out very, very much!


Here you go. Change your content in didSelectRowAtIndexPath to something like this:

NSArray *objectsToUse = nil;
if (tableView == [[self searchDisplayController] searchResultsTableView])
    objectsToUse = [self searchResults];
else
    objectsToUse = [self contentsList];
if ([[objectsToUse objectAtIndex:indexPath.row] isEqual:@"Hydrogen"])
{
    Hydrogen *hydrogen = [[Hydrogen alloc] initWithNibName:@"Hydrogen" bundle:nil];
    [self.navigationController pushViewController:hydrogen animated:YES];
    [hydrogen release];
}
else if ([[objectsToUse objectAtIndex:indexPath.row] isEqual:@"Helium"])
{
    Helium *helium = [[Helium alloc] initWithNibName:@"Helium" bundle:nil];
    [self.navigationController pushViewController:helium animated:YES];
    [helium release];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜