iOS 中 tableview 中的动态搜索 [英] Dynamic searching in tableview in iOS

查看:28
本文介绍了iOS 中 tableview 中的动态搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 tableview 和文本字段,我在其中实现了搜索方法.现在,当我在 textfield 中写入一些值并单击搜索按钮时,它会在 tableview 中搜索.但是,我希望它是动态的,这意味着当我开始输入 textfield 时,它应该开始搜索而无需单击任何 button.

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.

我该怎么做?

推荐答案

只需在 TextFieldEditing Changed Event 上连接此方法.因此,当您更改其值时会调用此方法.

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.
}

这里,allData 是一个 array,其中包含您的所有数据.而searchArray 是一个数组,其中只包含搜索数据.所以请确保,您必须将 searchArray 的计数设置为 tableview's 行计数.并根据这个searchArray将数据设置到tableview中.

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.

这篇关于iOS 中 tableview 中的动态搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆