Nilesh Jha Nilesh Jha - 2 months ago 7
Objective-C Question

Dynamic searching in tableview in iOS

I've a

tableview
and text field where I've implemented search method. Right now, when I write some value in
textfield
and click search button, then it search in the
tableview
. But, I want it to be dynamic means the moment I start typing in
textfield
it should start searching without clicking any
button
.

How can I do this?

Answer

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.