如何使用Obejctive C在tableview单元格中搜索特定值时获取所有数组值?

如何使用Obejctive C在tableview单元格中搜索特定值时获取所有数组值?

问题描述:

我已经实现了一个带有两个标签的自定义单元格的TableView来填充城市名称和城市ID(我保持城市ID标签隐藏)。在这里,我的问题是当我搜索城市名称时,我也无法获得城市ID,当我搜索城市名称时,我希望这两个值都被过滤。如何使用Obejctive C在tableview单元格中搜索特定值时获取所有数组值?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
    static NSString *[email protected]"citylocation"; 
     searchTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellidentifier]; 
     if (!cell) { 
      cell= [[searchTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellidentifier]; 
     } 
     if(!isFilltered) 
     { 
      cell.textLabel.text = [avahotel objectAtIndex:indexPath.row]; 
      cell.city.text = [[createdDate objectAtIndex:indexPath.row]stringValue]; 
     }else 
     { 
      cell.textLabel.text = [filteredString objectAtIndex:indexPath.row]; 
    }  
    return cell; 
} 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if(isFilltered) 
    { 
     return [filteredString count]; 
    } 
    return [avahotel count]; 
} 
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText 
{ 
    if(searchText.length == 0) 
    { 
     isFilltered = NO; 
    }else 
    { 
     isFilltered = YES; 
     filteredString = [[NSMutableArray alloc]init]; 
     for(NSString *str in avahotel) 
     { 
      NSRange stringRange = [str rangeOfString:searchText options:NSCaseInsensitiveSearch]; 

      if(stringRange.location != NSNotFound) 
      { 
       [filteredString addObject:str]; 
      } 
     } 
    } 
    [_searchtableview reloadData]; 
} 
+0

我有一个问题在这里。你想在filteredString数组中获得关于城市ID的信息吗?当真正搜索城市名称?还是我误解了? – Elena0987

+0

搜索城市名称和所有工作正常,我的需要是,如何过滤这两个标签。例如,如果我正在搜索钦奈的手段,我希望钦奈和城市id(100)值。 –

在该方法的其他部分使用以下代码。搜索栏textDidChange

更新

isFilltered = YES; 
    filteredString = [[NSMutableArray alloc]init]; 
    filteredCityId = [[NSMutableArray alloc]init]; 

    for(Int i=0; i<avahotel.count; i++) 
    { 
     NSString *str = [avahotel objectAtIndex:i]; 
     NSRange stringRange = [str rangeOfString:searchText options:NSCaseInsensitiveSearch]; 

     if(stringRange.location != NSNotFound) 
     { 
      [filteredString addObject:[avahotel objectAtIndex:i]]; 
      NSString *strId = [NSString stringWithFormat:@"%d",i]; 
      [filteredCityId addObject:strId] 

      // here your both filter array declare.filteredcityId and filteredString   
     } 
    } 
+0

这个类别不是关键值编码兼容的关键cityname ...我得到这个错误.avahotel是我的数组,其中包含所有cityname –

+0

你可以在这里分享avahotel(“打印数据响应”)数组?所以我会得到解决这个错误。 –

+0

可用酒店( AGRA, 班加罗尔 奈 GOA, HYDERABAD, JAIPUR, KOLKATA, “MUMBAI/BOMBAY”, “新德里”, 阿加尔塔拉, AHMEDABAD, 艾藻尔, 阿杰梅尔, 阿科拉, 阿里巴格, ALLAHABAD, 阿勒皮, 阿尔莫拉, 阿拉斯索哈维, 阿尔瓦尔, 安巴拉, AMLA, AMRITSAR, 阿南德 ANKLESWAR, ARONDA, ASHTAMUDI, } –