2012-02-15 120 views
2

我有一個搜索欄搜索數組,並更新與結果的UITableView。表視圖是一個書單,加標題,作者:UISearchBar搜索兩個陣列

The titles and authors

眼下,在搜索欄中只搜索標題,但我想,使搜索作者爲好。這裏是我的搜索代碼(我從http://blog.webscale.co.in/?p=228得到)。

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText 
{ 
    [tableData removeAllObjects];// remove all data that belongs to previous search 
    if([searchText isEqualToString:@""]||searchText==nil){ 
     [tableView reloadData]; 
     return; 
    } 

    for(NSString *name in dataSource){ 
     NSInteger counter = 0; 

     //NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init]; 
     NSRange r = [[name lowercaseString] rangeOfString:[searchText lowercaseString]]; 
     if(r.location != NSNotFound) 
      [tableData addObject:name]; 


      counter++; 
    } 
     //[pool release]; 


    [tableView reloadData]; 

}

數據源是NSMutable Array包含的標題。包含作者的數組稱爲「作者」。 「tableData」是存儲應該出現在屏幕上的單元格(包含要搜索的術語的單元格)的數組。

非常感謝,

盧克

回答

8

我會修改數據源陣列通過鍵值對(Book類會更好)創建一個NSDictionary同時包含標題和作者。

//Do this for each book 
NSDictionary * book = NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys: 
    title, @"TITLE", author, @"AUTHOR", nil]; 
[dataSource addObject:book]; 

之後,你可以改變你的搜索方法,而不是使用NSDictionary。

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

    [tableData removeAllObjects]; 

    if(searchText != nil && ![searchText isEqualToString:@""]){ 

     for(NSDictionary * book in dataSource){ 
      NSString * title = [book objectForKey:@"TITLE"]; 
      NSString * author = [book objectForKey:@"AUTHOR"]; 

      NSRange titleRange = [[title lowercaseString] rangeOfString:[searchText lowercaseString]]; 
      NSRange authorRange = [[author lowercaseString] rangeOfString:[searchText lowercaseString]]; 

      if(titleRange.location != NSNotFound || authorRange.location != NSNotFound) 
       [tableData addObject:book]; 
      } 

    } 

    [tableView reloadData]; 
} 

注意使用這種方法,你將有變化你的cellForRowAtIndexPath方法與NSDictionary中,而不是標題字符串工作。

+0

完美的答案@Joel克拉維茨 – Jashu 2016-03-17 05:19:04

1
-(void)searchBar:(UISearchBar *)searchBar1 textDidChange:(NSString *)searchText 
{ 
if ([searchText length]==0) 
{ 
    temp_array1 =[array_Main1 mutableCopy]; 
    temp_array2 =[array_Main2 mutableCopy]; 
    temp_array3 =[array_Main3 mutableCopy]; 
} 
else 
{ 
    [temp_array1 removeAllObjects]; 
    [temp_array2 removeAllObjects]; 
    [temp_array3 removeAllObjects]; 
    int g = 0; 
    for (int i=0; i< array_Main1.count;i++) 
    { 
     NSRange Range1 = [[array_Main1 objectAtIndex:i] rangeOfString:searchText options:NSCaseInsensitiveSearch]; 
     NSRange Range2 = [[array_Main2 objectAtIndex:i] rangeOfString:searchText options:NSCaseInsensitiveSearch]; 
     NSRange Range3 = [[array_Main3 objectAtIndex:i] rangeOfString:searchText options:NSCaseInsensitiveSearch]; 
     if (Range1.location != NSNotFound || Range2.location != NSNotFound || Range3.location != NSNotFound) 
     { 
      [temp_array1 addObject:[array_Main1 objectAtIndex:g]]; 
      [temp_array2 addObject:[array_Main2 objectAtIndex:g]]; 
      [temp_array3 addObject:[array_Main3 objectAtIndex:g]]; 

     } 
     g++; 
    } 
} 
[table reloadData]; 
} 
0
- This is Helpful when you search from Dictionary. 

NSMutableArray *contentList; 
NSMutableArray *filteredContentList; 
BOOL isSearching; 

// firstSection is array which already filled. 
// contentList array for value of particular key 
// filteredContentList is search array from actual array. 

    - (void)searchTableList { 
     NSString *searchString = searchBar.text; 

     NSPredicate *filterPredicate = [NSPredicate predicateWithFormat:@"frame_code beginswith[c] %@", searchString]; 
     NSArray *filteredArr = [firstSection filteredArrayUsingPredicate:filterPredicate]; 
     if(contentList.count > 0) 
      [contentList removeAllObjects]; 
     [filteredContentList addObjectsFromArray:filteredArr]; 
    } 

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

     if ([searchBar1.text length] != 0) 
      isSearching = YES; 
     else 
      isSearching = NO; 
    } 

    - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { 
     NSLog(@"Text change - %d",isSearching); 

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

     if([searchText length] != 0) { 
      isSearching = YES; 
      [self searchTableList]; 
     } 
     else { 
      isSearching = NO; 
     } 
     [tblFrameList_SComplete reloadData]; 
    } 

    - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar { 
     NSLog(@"Cancel clicked"); 
    } 

    - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar { 
     NSLog(@"Search Clicked"); 
     [self searchTableList]; 
    }