現在,我正在開發一個由表格視圖和搜索欄組成的應用程序。當有人點擊桌子上的某一行時,它將加載一個具有相應頁面的新視圖。這是我的代碼:UITableView搜索行不對應
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文件,而不進行搜索。例如,如果我在搜索中鍵入單詞「氦」,它將顯示一個結果「氦」,但當我點擊結果加載「氫」頁時,因爲氫是在第一行被點擊時訪問的原始鏈接。
任何人都可以幫助我嗎?我在這段代碼上花了很多時間,而且真的讓人感到沮喪。
非常感謝那些能夠幫助我的人!
在numberOfRowsInSection和的cellForRowAtIndexPath,你看這取決於條件或者SearchResult所或contentsList。在didSelectRowAtIndexPath中,你只能看到contentsList。這是爲什麼? – Anna 2011-03-07 03:55:31
@aBitObvious我剛剛開始學習iPhone開發大約一個月前,雖然我之前做過很多網絡開發,所以如果我聽起來很愚蠢,請原諒我;我仍然是一個新手。你所說的話很有意義(我的代碼已經從3個不同的來源大量修改過,所以我之前沒有注意到)。我可以添加一個if語句到didSelectIndexRowPath,但是我怎麼會說出我的邏輯?這是我感到困惑的最大的事情。任何幫助? – 2011-03-07 03:59:27