2012-04-23 22 views
2

我有uitableview和搜索欄與searchDisplayController,我填充tableview數據源與數據庫中的數據和我用戶搜索欄搜索,一次uitableview數據源不過濾didSelectRowAtIndexPath工作正常它進入detailviewcontroller,但是當我使用搜索和篩選數據源,然後如果我選擇一個單元didSelectRowAtIndexPath方法不會觸發。didSelectRowAtIndexPath不起作用,當過濾uitableview數據源

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    KardexViewController* kardexViewController = [[KardexViewController alloc] initWithStyle:UITableViewStylePlain]; 

    [self.navigationController pushViewController:kardexViewController animated:YES]; 
} 

這是怎麼了我添加的UISearchBar

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    dataSource = [[NSMutableArray alloc] init]; 

    self.navigationItem.title = Title; 

    searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; 

    searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self]; 
    searchDisplayController.delegate = self; 
    searchDisplayController.searchResultsDataSource = self; 

    self.tableView.tableHeaderView = searchBar; 
} 

,我該如何篩選數據源

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString 
{ 
    NSMutableArray* result = [[NSMutableArray alloc] init]; 

    for (int i = 0; i < [self.allData count]; i++) { 
     NSRange range = [[[self.allData objectAtIndex:i] valueForKey:@"Code"] rangeOfString:searchString]; 

     if(range.length > 0) 
      [result addObject:[self.allData objectAtIndex:i]]; 

     range = [[[self.allData objectAtIndex:i] valueForKey:@"Name"] rangeOfString:searchString]; 

     if(range.length > 0) 
      [result addObject:[self.allData objectAtIndex:i]]; 
    } 

    self.dataSource = [result copy]; 
    [result release]; 
    [self.searchDisplayController.searchResultsTableView reloadData]; 

    return YES; 
} 

-(void) searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller 
{ 
    self.dataSource = [self.allData copy]; 
} 

所以感謝inadvance

+0

請提供您所使用的搜索方式和文件服務器上的表,它還會有一些問題,因此它不正確或根本 – 2012-04-23 13:13:50

回答

17

經過一番搜索,我發現我只需要設置一個還有一點

searchDisplayController.searchResultsDelegate = self; 
+1

謝謝回來,並回答自己的問題的工作代碼。我一直在想,searchDisplayController.delegate = self;是足夠的,不是,我需要添加你的代碼,現在一切都很好。 – Ryan 2014-02-18 11:46:34

相關問題