UITableView搜索行不对应

时间:2011-03-07 03:41:37

标签: xcode uitableview uiview row

现在,我正在开发一个由表视图和搜索栏组成的应用程序。当有人点击表格中的一行时,它将加载一个具有相应页面的新视图。这是我的代码:

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 viewDidUnload];

    // 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

这个代码完全有效,除了一个缺陷。当我使用搜索时,结果显示为预期,结果与输入的字符串匹配。但是,当我点击搜索结果的其中一行时,相应的XIB文件不会加载;相反,它在没有搜索的情况下加载与UITableView中的原始文本相对应的XIB文件。

例如,如果我在搜索中输入单词“Helium”,它会显示一个结果“氦气”,但是当我点击结果时它会加载“氢气”页面,因为氢是点击第一行时访问的原始链接。

任何人都可以帮我吗?我在这段代码上花了很多天,实在令人沮丧。

感谢那些可以非常非常帮助我的人!

1 个答案:

答案 0 :(得分:2)

你走了。将didSelectRowAtIndexPath中的内容更改为以下内容:

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];
}