지금 당장 검색 막대가있는 테이블보기로 구성된 응용 프로그램을 개발 중입니다. 누군가가 테이블의 행 중 하나를 누르면 해당 페이지가있는 새로운보기가로드됩니다. 이건 내 코드입니다 :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 파일을로드합니다.
예를 들어 검색에서 "헬륨"단어를 입력하면 "헬륨"이라는 결과가 표시되지만 수소 결과가 나오면 "수소"페이지가로드됩니다. 첫 번째 행을 두드렸을 때 액세스 된 원래 링크
아무도 도와 줄 수 있습니까? 나는이 코드에 대해 많은 시간을 보냈으며, 실제로 실망하고 있습니다.
누가 나를 도와 줄 수있는 사람을 매우 감사합니다!
, 당신의 SearchResult 또는 contentsList 중 하나가 조건에 따라 봐. didSelectRowAtIndexPath에서는 contentsList 만 봅니다. 왜 그런가요? – Anna
@aBitObvious 방금 전 웹 개발을 많이 했었지만 한 달 전에 iPhone 개발을 배우기 시작 했으므로 어리 석다면 실례합니다. 나는 아직 초보자입니다. 당신이 말하는 것은 많은 의미가 있습니다. (코드는 3 가지 소스에서 많이 수정되었으므로 이전에는 알지 못했습니다.) didSelectIndexRowPath에 if 문을 추가 할 수는 있지만 논리는 어떻게 표현할 수 있습니까? 이것은 내가 혼란스러워하는 가장 큰 것입니다. 어떤 도움이 필요합니까? –