我有一个包含很多行的 TableView,大多数在加载 viewController 时都不可见。UITableView 的行是从 SQLite 数据库中提取的。如何在所有行之间进行 SearchBar 搜索,而不仅仅是可见行?
在头文件中:
 @interface ExhibitorsViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate>{
BOOL isSearchOn;
BOOL canSelectRow;
NSMutableArray * listOfExpositors;
NSMutableArray * searchResult;
}
 //....
 @end
在实现文件中
 -(NSInteger)tableView:(UITableView *)tableView
  numberOfRowsInSection:(NSInteger)section
{
     if(isSearchOn){
           return [searchResult count];
           //In this array there are the elements after use of searchBar
     }else{
          int number=[self.mutableArray count];
         //In this array there are all elements of database, extracts in viewDidLoad()
       return number;
     }
}
 - (UITableViewCell *)tableView:(UITableView *)aTableView
     cellForRowAtIndexPath:(NSIndexPath *)indexPath {
       static NSString *CellIdentifier = @"Cell";
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
         if (cell == nil) {
           cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
           reuseIdentifier:CellIdentifier] ;
}
       if(isSearchOn){
            NSString * cellValue = [searchResult objectAtIndex:indexPath.row];
           cell.textLabel.text = cellValue;
      }else{
           //loading data from the database
           Database *currow =[self.mutableArray objectAtIndex:indexPath.row];
           cell.textLabel.text = currow.name;
         [listOfExpositors addObject:cell.textLabel.text];
         //here only loads the list of names visible and not the entire table
         //How do I put all the elements in this array?
         NSLog(@" %@", listOfExpositors);
         isSearchOn = NO;
         canSelectRow = YES;
  }
}
 -(void) doneSearching{
      isSearchOn = NO;
      canSelectRow = YES;
      self.tableView.scrollEnabled = YES;
     [self.searchBar resignFirstResponder];
     [self.tableView reloadData];
 }
  -(void) searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
        isSearchOn = YES;
        if(self.searchBar.text.length>0){
            canSelectRow=YES;
            self.tableView.scrollEnabled = YES;
        }else{
            canSelectRow= NO;
            self.tableView.scrollEnabled = NO;
        }
    }
   -(void) searchExpositorsTableView{
         [searchResult removeAllObjects];
         for (NSString *str in listOfExpositors){
              NSRange titleResultsRange = [str rangeOfString:self.searchBar.text options:
               NSCaseInsensitiveSearch];;
              if (titleResultsRange.length >0) {
                      [searchResult addObject:str];
              }
          }
        NSLog(@"%@", searchResult);
      }
    -(void) searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
             if([searchText length]>0){
                     canSelectRow = YES;
                     self.tableView.scrollEnabled = YES;
                     [self searchExpositorsTableView];
             }else{
                    canSelectRow = NO;
                    self.tableView.scrollEnabled = NO;
              }
             [self.tableView reloadData];
        }
      -(void) searchBarSearchButtonClicked:(UISearchBar *)searchBar{
            [self searchExpositorsTableView];
            [self.searchBar resignFirstResponder];
      }
   -(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{
        if(canSelectRow){
            return indexPath;
        }else{
              return nil;
       }
      NSLog(@"%d", indexPath.row);
    }