0

谁能帮我找出为什么在搜索栏中输入文本时调用搜索栏委托方法时我无法获取过滤后的数组

textDidChange的工具栏方法

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
     if(searchText.length == 0){
        isFiltered = NO;
     }else{
         isFiltered = YES;
         [filterdArray removeAllObjects];
         for(int i = 0; i < [productArray count]; i++){
              NSRange textRange;
              textRange =[[[[productArray objectAtIndex:i] objectForKey:@"senior_name"] lowercaseString] rangeOfString:[searchText lowercaseString]];

              if(textRange.location != NSNotFound){
                  [filterdArray addObject:[productArray objectAtIndex:i]];
                  NSLog(@"filterdArrayyyyyyyy:%@",filterdArray);
              }
         }
     }

     [self.residentListTableView reloadData];   
}

这是我productArray

(
    {
    id = 369;
    "room_no" = 101;
    "senior_name" = Tim;
},
    {
    id = 388;
    "room_no" = "<null>";
    "senior_name" = res444;
},
    {
    id = 382;
    "room_no" = "<null>";
    "senior_name" = tt1234;
},......

filterarray正在返回 null,

我想根据“senior_name”过滤表视图

先感谢您,

4

3 回答 3

2

您可以尝试下面的代码来获得适当的结果。

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
 if(searchText.length == 0){
    isFiltered = NO;
 }else{
     isFiltered = YES;
     [filterdArray removeAllObjects];
     for(int i = 0; i < [productArray count]; i++){
        if([[[[productArray objectAtIndex:i] objectForKey:@"senior_name"] lowercaseString] rangeOfString:[searchText lowercaseString]].length>0){
            [filterdArray addObject:[productArray objectAtIndex:i]];
            NSLog(@"filterdArrayyyyyyyy:%@",filterdArray);
        }
    }
 }

 [self.residentListTableView reloadData];   
 }
于 2017-03-17T08:59:35.060 回答
0

对于这个问题的新观众@shelby 没有在 viewDidLoad 中初始化数组,他只是声明了它。

拇指规则相同

声明 NSMutableArray *filterdArray ; // 这只是声明

初始化 [[NSMutableArray alloc]init][NSMutableArray new]

于 2017-10-10T09:09:11.670 回答
0

在 searchBar 委托方法中添加以下代码-

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

 NSString *textToSearch = [searchBar.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]; // Its better to remove whitespaces & newline characters

 isFiltered = textToSearch.length ? YES : NO;

NSPredicate *searchPredicate = [NSPredicate predicateWithFormat:@"self.senior_name MATCHES[cd] %@",textToSearch]; // This predicate will search for exact case-insensitive match. You can change your predicate also. 
filteredArray = [productArray filteredArrayUsingPredicate: searchPredicate];

[filteredArray removeAllObjects];
if (shouldShowSearchResults && filteredArray.count == 0) {        
   // Show a view like no search results found        
}
else {
    [self.residentListTableView reloadData];
  }    
 }

在您的 tableView 委托方法中添加以下内容 -

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return (isFiltered ? filteredArray.count : productArray.count);
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell =  [tableView dequeueReusableCellWithIdentifier:CELL_ID];

if(isFiltered){
  // Load data from filteredArray
}else{
// Load data from productArray
    }
  return cell;
}
于 2017-03-21T09:58:46.110 回答