0

我有一个包含很多行的 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);
    }
4

1 回答 1

0

使用在cellForRowAtIndexPath中创建的数组是错误的,因为它仅限于可见元素。然后我使用了在 viewDidLoad() 中创建的 NSMutableArray,它确实包含了数据库的所有元素。我更改了以下方法:

 - (UITableViewCell *)tableView:(UITableView *)aTableView
     cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    //....

   if(isSearchOn){
       NSString * cellValue = [searchResult objectAtIndex:indexPath.row];
       cell.textLabel.text = cellValue;
   }else{
      Database *currow =[self.mutableArray objectAtIndex:indexPath.row];


       cell.textLabel.text = currow.name;

     //Should be removed  this array and used to another, self.mutableArray

    //[listOfExpositors addObject:cell.textLabel.text];
    // NSLog(@" %@", listOfExpositors);

    isSearchOn = NO;
    canSelectRow = YES;
  }
}

研究方法

 -(void) searchExpositorsTableView{
      [searchResult removeAllObjects];

      for (Database *currow in self.mutableArray){ 
             /*In self.mutableArray there are all elements because is created in
               viewDidLoad*/
             NSLog(@"list of expositors %@", self.mutableArray);

             NSRange titleResultsRange = [currow.name rangeOfString:self.searchBar.text options: NSCaseInsensitiveSearch];
             if (titleResultsRange.length >0) {
                  [searchResult addObject:currow.name];

             }
        }

       NSLog(@"%@", searchResult);
     }
于 2013-01-11T08:24:37.003 回答