2011-02-16 5 views
2

tableview의 검색 창에 XML을 추가하는 방법에 대한 질문이 있습니다. 내가 tableview에서로드 할 수있는 모든 외부 XML 파일을 얻을 수 있지만 상단에 검색 창을 쳐서 편지를 누르십시오, 그것은 충돌합니다.iPhone TableView 검색 XML

내가 잘못하고있는 것이 정말 간단하다고 생각합니다. 내 RootViewController에는 searchTableView라는 함수가 있습니다. 나는 그것이 검색 항목을 집어 들고 있지 않은 곳인 것처럼 느낍니다. 나는 그것이 objectForKey : @ "title"주위의 어딘가에 있다고 생각한다. 디버깅 할 때이 오류 메시지도 : "NSCFArray objectForKey unrecognized selector". 내 searchTableView 함수는 다음과 같습니다.

- (void) searchTableView { 

    NSString *searchText = searchBar.text; 
    NSMutableArray *searchArray = [[NSMutableArray alloc] init]; 

    for (NSDictionary *dictionary in listOfItems) 
    { 
     NSArray *array = [dictionary objectForKey:@"title"]; 
     [searchArray addObjectsFromArray:array]; 
    } 

    for (NSString *sTemp in searchArray) 
    { 
     NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch]; 

     if (titleResultsRange.length > 0) 
      [copyListOfItems addObject:sTemp]; 
    } 

    [searchArray release]; 
    searchArray = nil; 
} 

답변

1

잘 알아 냈습니다. 어떤 이유로이 작업을 수행하는 방법을 문서화하기가 정말 어려웠습니다.

다음은 내 RootViewController.m입니다.

내 PLIST는 다음과 같이 구성되어 있습니다 :

  • 루트 (배열)
  • Item0 (사전)
  • 이름 (문자열)
  • 항목 1 (사전)
  • 이름 (문자열) ..

여기 내 코드가 있습니다.이 코드는 도움이 필요한 사람에게 도움이됩니다. :

@implementation RootViewController 
@synthesize listOfItems, copyListOfItems; 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    //Initialize the array. 
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"plistArray" ofType:@"plist"]; 
    NSMutableArray* tmpArray = [[NSMutableArray alloc] initWithContentsOfFile:filePath]; 
    self.listOfItems = tmpArray; 
    [tmpArray release]; 

    //Initialize the copy array. 
    copyListOfItems = [[NSMutableArray alloc] init]; 

    //Set the title 
    self.navigationItem.title = @"Search"; 

    //Add the search bar 
    self.tableView.tableHeaderView = searchBar; 
    searchBar.autocorrectionType = UITextAutocorrectionTypeNo; 

    searching = NO; 
    letUserSelectRow = YES; 
} 



- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview 
    // Release anything that's not essential, such as cached data 
} 

#pragma mark Table view methods 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 

    if (searching) 
     return 1; 
    else 
     return 1; 
} 

// Customize the number of rows in the table view. 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

    if (searching) 
     return [copyListOfItems count]; 
    else { 

     //Number of rows it should expect should be based on the section 
     return [listOfItems count]; 
    } 
} 


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    //Get the selected country 

    NSString *selectedCountry = nil; 

    if(searching) 
     selectedCountry = [copyListOfItems objectAtIndex:indexPath.row]; 
    else { 

     // Navigation logic may go here. Create and push another view controller. 

    } 

    NSDictionary *dictionary = [self.listOfItems objectAtIndex:indexPath.row]; 
    FoodDetail *dvController = [[FoodDetail alloc] initWithNibName:@"FoodDetail" bundle:nil andDictionary: dictionary]; 

    // Pass the selected object to the new view controller. 
    [self.navigationController pushViewController:dvController animated:YES]; 
    [dvController release]; 

} 

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    // Set up the cell... 

    if(searching) 
     cell.textLabel.text = [copyListOfItems objectAtIndex:indexPath.row]; 
    else { 

     cell.textLabel.text = [[self.listOfItems objectAtIndex:indexPath.row] 
           objectForKey:@"Name"]; 
    } 

    return cell; 
} 



- (NSIndexPath *)tableView :(UITableView *)theTableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    if(letUserSelectRow) 
     return indexPath; 
    else 
     return nil; 
} 





#pragma mark - 
#pragma mark Search Bar 

- (void) searchBarTextDidBeginEditing:(UISearchBar *)theSearchBar { 

    //This method is called again when the user clicks back from the detail view. 
    //So the overlay is displayed on the results, which is something we do not want to happen. 
    if(searching) 
     return; 

    //Add the overlay view. 
    if(ovController == nil) 
     ovController = [[OverlayViewController alloc] initWithNibName:@"OverlayView" bundle:[NSBundle mainBundle]]; 

    CGFloat yaxis = self.navigationController.navigationBar.frame.size.height; 
    CGFloat width = self.view.frame.size.width; 
    CGFloat height = self.view.frame.size.height; 

    //Parameters x = origion on x-axis, y = origon on y-axis. 
    CGRect frame = CGRectMake(0, yaxis, width, height); 
    ovController.view.frame = frame;  
    ovController.view.backgroundColor = [UIColor grayColor]; 
    ovController.view.alpha = 0.5; 

    ovController.rvController = self; 

    [self.tableView insertSubview:ovController.view aboveSubview:self.parentViewController.view]; 

    searching = YES; 
    letUserSelectRow = NO; 
    self.tableView.scrollEnabled = NO; 

    //Add the done button. 
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] 
               initWithBarButtonSystemItem:UIBarButtonSystemItemDone 
               target:self action:@selector(doneSearching_Clicked:)] autorelease]; 

} 

- (void)searchBar:(UISearchBar *)theSearchBar textDidChange:(NSString *)searchText { 

    //Remove all objects first. 
    [copyListOfItems removeAllObjects]; 

    if([searchText length] > 0) { 

     [ovController.view removeFromSuperview]; 
     searching = YES; 
     letUserSelectRow = YES; 
     self.tableView.scrollEnabled = YES; 
     [self searchTableView]; 
    } 
    else { 

     [self.tableView insertSubview:ovController.view aboveSubview:self.parentViewController.view]; 

     searching = NO; 
     letUserSelectRow = NO; 
     self.tableView.scrollEnabled = NO; 
    } 

    [self.tableView reloadData]; 
} 

- (void) searchBarSearchButtonClicked:(UISearchBar *)theSearchBar { 

    [self searchTableView]; 
} 

- (void) searchTableView { 

    NSString *searchText = searchBar.text; 
    NSMutableArray *searchArray = [[NSMutableArray alloc] init]; 

    for (NSDictionary *dictionary in listOfItems) 
    { 
     NSString *text1 = [dictionary objectForKey:@"Name"]; 
     [searchArray addObject:text1]; 
    } 
    NSLog(@"%s: searchArray=%@", __func__, searchArray); 

    for (NSString *sTemp in searchArray) 
    { 
     NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch]; 

     if (titleResultsRange.length > 0) 
      [copyListOfItems addObject:sTemp]; 
    } 

    [searchArray release]; 
    searchArray = nil; 
} 

- (void) doneSearching_Clicked:(id)sender { 

    searchBar.text = @""; 
    [searchBar resignFirstResponder]; 

    letUserSelectRow = YES; 
    searching = NO; 
    self.navigationItem.rightBarButtonItem = nil; 
    self.tableView.scrollEnabled = YES; 

    [ovController.view removeFromSuperview]; 
    [ovController release]; 
    ovController = nil; 

    [self.tableView reloadData]; 
} 


- (void)dealloc { 

    [ovController release]; 
    [copyListOfItems release]; 
    [searchBar release]; 
    [listOfItems release]; 
    [super dealloc]; 
} 


@end