Dynamic searching in tableview in iOS

笑着哭i 提交于 2019-12-05 18:27:59

Just Connect this method on Editing Changed Event of your TextField. So, this method is called when you are changing its value.

- (IBAction)txtValueChanged:(UITextField)sender
{
    [self dynamicSearchInto:sender.text];
    //From here, we are passing text of textField.
}

Here, allData is an array which contains all your data. And searchArray is an array in which contains only searching data. So make sure that, you have to set count of searchArray into tableview's rows count. And setting data into tableview according to this searchArray.

-(void)dynamicSearchInto:(NSString *)strText
{
    if (strText.length != 0)
    {
        searchArray = [[NSArray alloc]init];
        NSString *str=txtSearch.text;
        NSPredicate *predicate=[NSPredicate predicateWithFormat:@"SELF['yourKey'] contains[c] %@", str];
        searchArray = [allData filteredArrayUsingPredicate:predicate];
        [myTableView reloadData];
    }
    else
    {
        searchArray = allData;
    }
    [myTableView reloadData];
}

Hope, this is what you're looking for. Any concern get back to me.

To implement dynamic search in the ios, we can also use textbox as search bar.

Go to StoryBoard and do the following changes

  1. Add the textbox to the view controller and assign the outlets and delegates.
  2. now to viewcontroller.m and paste the following code

-(void)textFieldDidChange:(UITextField *)textField
{
    searchTextString=textField.text;

    [self updateSearchArray:searchTextString];
}


-(void)updateSearchArray:(NSString *)searchText
{
    
    if(searchText.length==0)
   {
        isFilter=NO;    }
    else{
      
       isFilter=YES;
        
       searchArray=[[NSMutableArray alloc]init];
        
        for(NSString *string in responceArray){

        NSRange stringRange = [[NSString stringWithFormat:@"%@",string]
                               rangeOfString:searchtext.text options:NSCaseInsensitiveSearch];
            
        if (stringRange.location != NSNotFound) {
            [searchArray addObject:string];

        }
            
        }
        
        [jobsTable reloadData];
    }
    
    }

Connect method that do the same as on click to the event Value changed (see in the screenshot at the very bottom) in connections inspector.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!