gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:31:12 24 4
gpt4 key购买 nike

我已经实现了一个带有自定义单元格的 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];
}

最佳答案

在这个方法的 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
}
}

关于ios - 使用 Obejctive C 在 tableview 单元格中搜索特定值时如何获取所有数组值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46989306/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com