在我的项目中我正在使用UITextField,我必须将其用作UISearchBar,因为当我开始键入UITableView时将打开并显示搜索字符串。
我有一个名为字典的NSDictionary,看起来像
{
268 = "David b Augustat";
449 = "Rocky Balboa";
489 = "Test Aayush";
493 = "Mohit Driver";
494 = "Tst ";
495 = "Mohit DriverA";
496 = "Test Kailash Driver";
497 = "Pramod Sinha";
498 = "Tty Ffff";
}
和两个NSArray DrivernameArray和DriverIdArray,其中包含字典中的名称和ID
当我在tableview中显示搜索结果时,按名称搜索工作正常并显示正确的结果但是具有该名称的关联ID不会随名称更新它始终保持相同
我正在使用以下代码
NSDictionary *listing = [dict6 objectForKey:@"result"];
DriverNameArray = [listing valueForKey:@"full_name"];
DriverIdArray = [listing valueForKey:@"id"];
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
tempArray = [NSArray arrayWithArray:DriverNameArray];
//NSString *stringTovSearch = textField.text;
tempId = [NSArray arrayWithArray:DriverIdArray];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF BEGINSWITH[cd] %@",driverTf.text]; // if you need case sensitive search avoid '[c]' in the predicate
NSArray *tempresults = [DriverNameArray filteredArrayUsingPredicate:predicate];
if (tempresults.count > 0)
{
tempArray = [NSArray arrayWithArray:tempresults];
tempId = [NSArray arrayWithArray:tempresults];
}
[SEarchTable reloadData];
return YES;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: nil];
}
cell.textLabel.font=[UIFont fontWithName: @"Arial" size: 14.0 ];
cell.textLabel.numberOfLines = 0;
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
{
UILabel * idlbl = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 50, 10)];
[cell.contentView addSubview:idlbl];
idlbl.hidden = false;
if (tempArray.count > 0)
{
cell.textLabel.text = [tempArray objectAtIndex:indexPath.row];
idlbl.text = [DriverIdArray objectAtIndex:indexPath.row];
cell.textLabel.font=[UIFont fontWithName: @"Arial" size: 14.0 ];
}
else
{
idlbl.text = [DriverIdArray objectAtIndex:indexPath.row];
cell.textLabel.text = [DriverNameArray objectAtIndex:indexPath.row];
}
}
return cell;
}
答案 0 :(得分:0)
更改类型数组
NSMutableArray *tempArray,*tempId;
- (void)viewDidLoad {
[super viewDidLoad];
tempArray = [NSMutableArray new];
tempId = [NSMutableArray new];
}
过滤时
[tempArray removeAllObjects];
[tempId removeAllObjects]
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"full_name = %@",driverTf.text]; // if you need case sensitive search avoid '[c]' in the predicate
NSArray *mergedResultArray = [listing ]filteredArrayUsingPredicate:predicate];
// now different array
if (tempresults.count > 0)
{
tempArray = [mergedResultArray valueForKey:@"full_name]";
tempId = [mergedResultArray valueForKey:@"id"];
}
或您的代码的另一种可能解决方案
if (tempArray.count > 0)
{
cell.textLabel.text = [tempArray objectAtIndex:indexPath.row];
idlbl.text = [tempId objectAtIndex:indexPath.row]; // made changes here
cell.textLabel.font=[UIFont fontWithName: @"Arial" size: 14.0 ];
}
else
{
idlbl.text = [DriverIdArray objectAtIndex:indexPath.row];
cell.textLabel.text = [DriverNameArray objectAtIndex:indexPath.row];
}
答案 1 :(得分:0)
您的代码中存在多个问题。
过滤时,只分配包含名称的数组:
if (tempresults.count > 0)
{
tempArray = [NSArray arrayWithArray:tempresults]; // This seems to be used outside this method and in the cell for row method on access
tempId = [NSArray arrayWithArray:tempresults]; // This is a local variable and has no effect outside this mehod. Did you mean DriverIdArray =...
}
您实际上只过滤名称数组而不是ID数组
NSArray *tempresults = [DriverNameArray filteredArrayUsingPredicate:predicate];
假设DriverNameArray
有10个元素,然后DriverIdArray
必须有10个元素。过滤后的数组tempArray
可能有5个项目,而DriverIdArray
仍然有10个。您应该删除相同的索引。
有很多方法可以解决这个问题,我个人建议你使用一个包含名称和ID的自定义对象的单个数组。然后过滤那个数组。
为了快速修复,你可能只是在原始名称数组中找到你的名字对象的索引,并在ID数组中应用相同的索引(我不鼓励你这样做):
NSString *name = [tempArray objectAtIndex:indexPath.row];
NSInteger indexOfName = [DriverNameArray indexOf:name];
NSString *identifier = [DriverIdArray objectAtIndex:indexOfName];
编辑:代码
要将其插入到代码中,您只需要修改单元格中用于行方法的部分:
if (tempArray.count > 0)
{
NSString *name = [tempArray objectAtIndex:indexPath.row];
NSInteger indexOfName = [DriverNameArray indexOf:name];
NSString *identifier = [DriverIdArray objectAtIndex:indexOfName];
cell.textLabel.text = name;
idlbl.text = identifier;
cell.textLabel.font=[UIFont fontWithName: @"Arial" size: 14.0 ];
}