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

How to get all the array values when searching particular values in tableview cell using Obejctive C?

提问人:Shubam Gupta 提问时间:10/28/2017 最后编辑:KrunalShubam Gupta 更新时间:11/1/2017 访问量:92

问:

我已经实现了一个带有自定义单元格的 TableView,其中包含两个标签来填充城市名称和城市 ID(我隐藏了城市 ID 标签)。在这里,我的问题是当我搜索城市名称时,我也无法获得城市ID,当我搜索城市名称时,我希望过滤这两个值。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
     static NSString *cellidentifier=@"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];
}
iOS Objective-C UITableView iOS7

评论

0赞 Elena0987 10/29/2017
我这里有一个问题。是否希望在filteredString数组中获取有关城市ID的信息?在实际搜索城市名称时?还是我误会了?
0赞 Shubam Gupta 10/30/2017
搜索城市名称和一切正常,我需要的是,如何过滤两个标签。例如,如果我正在搜索 Chennai 意味着,我想要 Chennai 和 city id(100) 值。

答:

1赞 BuLB JoBs 10/29/2017 #1

在此方法的 else 部分中使用以下代码。searchBar 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赞 Shubam Gupta 11/1/2017
此类不符合键 cityname...我收到此错误.here avahotel是我的数组,其中包含所有cityname
0赞 BuLB JoBs 11/1/2017
你能在这里分享avahotel(“打印数据响应”)数组吗?所以我会得到这个错误的解决方案。
0赞 Shubam Gupta 11/1/2017
可用的酒店是(阿格拉、班加罗尔、钦奈、果阿、海得拉巴、斋浦尔、加尔各答、“孟买/孟买”、“新德里”、阿加尔塔拉、艾哈迈达巴德、艾扎尔、阿杰梅尔、阿科拉、阿里巴格、阿拉哈巴德、阿勒皮、阿尔莫拉、阿尔西萨尔、阿尔瓦尔、安巴拉、阿姆拉、阿姆利则、阿南德、安克尔斯瓦尔、阿隆达、阿什塔穆迪、}
0赞 Shubam Gupta 11/1/2017
城市 ID 为 {1,2,3,4,5,6,7,8,9....}如果我搜索 Chennai 意味着我还需要根据 cityID (3) ..
0赞 BuLB JoBs 11/1/2017
@ShubamGupta 好的,理解你的代码!我更新了我的代码,请使用它。希望对您有所帮助