2011-11-11 114 views
1

我有一個搜索欄,我現在可以搜索,但是當我輸入要搜索的文本,然後單擊取消按鈕。它並沒有讓我回到我的第一個階段,這意味着桌子上的物品已滿。取消按鈕在搜索欄上沒有正確取消

例如:我用單詞搜索項目:a,它給我所有的一個項目,是的,它現在是,但是當我點擊取消按鈕時,我想讓程序給我所有的項目存在,不只是一個項目。

這裏是代碼:請幫助我。非常感謝。

- (void)searchBarCancelButtonClicked:(UISearchBar *)aSearchBar 
{ 
    searchBar.text = @""; 
    [searchBar resignFirstResponder]; 

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

    NSLog(@"what text after cancel now: %@", searchBar.text); 

    [self.tableView reloadData]; 


} 
- (NSMutableArray *) searchTableView { 

    NSString *searchText = searchBar.text; 
    NSLog(@"search text: %@", searchText); 
    NSMutableArray *resultArray = [[NSMutableArray alloc] init]; 
    NSMutableArray *tempArr = [[NSMutableArray alloc] init]; 

    for (NSDictionary *dTemp in arrayData) 
    { 
     NSString *tempStr = [dTemp objectForKey:@"url"]; 
     NSLog(@"sTemp string: %@",[ NSString stringWithFormat:@"%@", tempStr]); 
     NSRange titleResultsRange = [tempStr rangeOfString:searchText options:NSCaseInsensitiveSearch]; 

     if (titleResultsRange.length > 0) 
     { 
      NSLog(@"1 count :%d", [resultArray count]); 
      [resultArray addObject:dTemp]; 
      NSLog(@"2 count :%d", [resultArray count]); 
      [tempArr addObject:resultArray]; 
      [resultArray release]; 

      resultArray = [NSMutableArray new]; 
     } 

    } 
    if (resultArray != nil) { 
     [resultArray release]; 
    } 


    return tempArr; 
} 

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

    NSLog(@"what text after cancel now: %@", searchBar.text); 

    if([searchText length] > 0) { 
     [sortedArray removeAllObjects]; 
     searching = YES; 
     letUserSelectRow = YES; 
     self.tableView.scrollEnabled = YES; 
     NSMutableArray *searchArray = [self searchTableView]; 
     sortedArray = [[NSMutableArray alloc] initWithArray:searchArray copyItems:YES]; 

     for (int i = 0; i<[sortedArray count]; i++) { 
      NSLog(@"this is the search array: %@", [[sortedArray objectAtIndex:i] class]); 
     } 

     NSLog(@"sorted array: %d", [sortedArray count]); 
    } 
    else { 

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

    [self.tableView reloadData]; 
} 

回答

0

您不需要重寫任何UISearchBar方法來完成此操作。這樣做的新方法取決於UISearchDisplay控制器(特別是在shouldReloadTableForSearchString上)。

聲明你的視圖控制器以符合UISearchDisplayDelegate協議,並保持兩個實例變量:你的模型作爲NSArray(所有數據)和一個濾波陣列NSMutableArray(您的數據的子集)。您目前在「searchTableView」中的代碼將過濾模型的內容並將其放入已過濾的NSMutableArray。然後,您將覆蓋以下UITableView方法:-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath。在返回之前,請進行比較以確定tableView參數是否等於self.searchDisplayController.searchResultsTableView。如果是,用戶正在查看已過濾的列表,並且您應該使用已過濾的NSMutableArray的內容來創建視圖,否則,用戶正在查看整個數據集,並且應該使用NSArray的內容來保存您的模型。看看下面的蘋果代碼的什麼我描述一個簡單的例子:

http://developer.apple.com/library/ios/#samplecode/TableSearch/Introduction/Intro.html

+0

這是一個老的文章,但只是爲了記錄UISearchDisplayDelegate在iOS8上棄用。現在適合使用UISearchControllerDelegate。 https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UISearchDisplayDelegate_Protocol/index.html – ThinkBonobo