【问题标题】:Ios: Use textfield as searchbarIos:使用文本字段作为搜索栏
【发布时间】:2017-07-20 08:37:01
【问题描述】:

在我的项目中,我使用的是 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,其中包含字典中的 name 和 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;
}

【问题讨论】:

  • 您需要合并数组 id 和名称,然后过滤该数组。

标签: ios objective-c iphone uitableview uisearchbar


【解决方案1】:

您的代码中有多个问题。

  1. 过滤时,您只分配包含名称的数组:

    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 =...
    }
    
  2. 您实际上只过滤 name 数组而不是 IDs 数组

    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 ];
        }

【讨论】:

  • No visible @interface for 'NSArray' 声明选择器 'indexOf:'
  • @AbhinandanPratap 您是否尝试过在网上搜索以查找它用于获取对象索引的方法?看起来 indexOfObject 可能就是这种情况。 SO 并不是为了让您获得将复制粘贴到项目中的代码!查看人们发布给您的代码和描述,理解代码,尝试实现代码!如果您有任何问题,请在完成一些研究后提出问题!
【解决方案2】:

更改类型数组

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];
        }

【讨论】:

  • 如何合并两个数组
  • 您能否添加代码来显示您为 DriverIdArray 分配值的位置。
  • 或者也尝试我提到的另一个解决方案。
  • 你从 json 得到日期了吗?
  • “NSDictionary”没有可见的@interface 声明选择器“filteredArrayUsingPredicate:”
猜你喜欢
  • 2014-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-26
  • 2012-01-03
  • 2015-01-12
相关资源
最近更新 更多