使用自定义搜索textField过滤tableView

时间:2013-09-05 07:08:24

标签: iphone ios objective-c

你好朋友在我的一个活动中我创建了自定义文本字段,我有数组有50条记录并为加载数组记录创建自定义表....现在我想要当我们输入文本字段然后我的数组记录应该在表格视图中过滤并获得欲望记录...

UITextField *patientname=[[UITextField alloc]initWithFrame:CGRectMake(130,10,170,30)];
patientname.font = [UIFont boldSystemFontOfSize:15.0];
patientname.tag=1;
//the horizontal alignment of the text
patientname.textAlignment = NSTextAlignmentLeft;
patientname.borderStyle = UITextBorderStyleBezel;
patientname.contentVerticalAlignment = UIControlContentHorizontalAlignmentCenter;
patientname.clearButtonMode = UITextFieldViewModeWhileEditing; 
patientname.delegate =self;
[Self.view addSubview:patientname];

这是我的阵列......

Array Name (
"Mr. Ram  Singh",
"Mr.   ",
"Master DSHKJLKD  null mfkmlkdfsg",
"Mr. Rajat  Jain",
"Mrs. Sonali K Singh",
"Mr. Amit  Singh",
"Mr. Paan  Singh",
"Mr. Ajit  Singh",
"Dr. Rohan  Sharma",
"Mr. Arpita  Tripathi",
"Mr. Jacky  Chain",
"Ms. Malini  Ramani",
"Mr. Ankit  Chavan",
"Mr. Ankit  Gupta",
"Ms. Ragini  Khanna",
"Mr. Raj  Kumar",
"Mr. Manish  gupta",
"Mr. Anshul  Aggarwal",
"Ms. Priya  Ranjith",
"Mr. Sdf Sdf Sdf",
"Mr. Sanyam  Jain",
"Mr. Rakesh  Gupta",
"Mr. Vishnu  Gupta",
"Mr. Shabbir K Malik",
"Mr. mohan  gupta",
"Mr. Sajat  Jain",
"Mr.   ",
"Mr. ravi  kapoor",
"Miss Mona  Hdjdbshs",
"Mr. test new  demo",
"Mr. Devdas  Gupta",
"Mr. Tarun  Malik",
"Mr. Mayank  Sharma",
"Miss Rachits  Guptas",
"Mr. Test T Patient",
"Mr. Manish  Kumar",
"Mr. Asd Asd Asd",
"Mr. NARENDRA  Garg",
"Mr. ritik  gupta",
"Mr. Mohd  Salim",
"Mr. Anshul  Garg",
"Ms. tanvi  anand",
"Mr. ashish  singh",
"Mr. Navdeep  Bora",
"Mr. Rajat  Kumar",
"Mr. shardul  negi",
"Mr. Shrey  Bhalla",
"Mr.   ",
"Mr. Dushyant  Sharma",
"Mr. Ritesh  Kapoor",
"Mr. Jamna  Das",
"Mr. hello  patient",
"Mr. Ashish  Vashisht",
"Ms. Prachi  Pant",
"Mr.   ",
"Mr. Sunil Kumar Singh",
"Mr. Mohd  Ali",
"Mr. Jay  Singh",
"Mr. Pantient  Patient",
"Mr. paitent  demo",
"Mr. adi  df",
"Mr. Anamika  Gupta",
"Mr. Subh  Karmakar",
"Mrs. Kirshna  Kumar",
"Mr. Jay  Kishan",
"Mr. Ram  Singh",
"Mr. Jagjeet  Singh",
"Mr. Badal  Yadav",
"Mr. Isha  Bhalla",
"Mr. Saadhna  Gupta",
"Mr. Tar  Un",
"Mr. SR  Khan",
"Mr. amar  nath",
"Mr. ankur  kaul",
"Mr. Qwe Qwe Qwe",
"Ms. Fgh Fgh Fgh",
"Mr. Kishan  Lal",
"Mrs. Kkkk Kkk Kkk",
"Mr. radha  gupta",
"Mrs. Meena  Sharma",
"Mr. Charu  Jain",
"Mr. dsf  df",
"Mr. Testdemo  Demotest",
"Mr.   ",
"Mr.   ",
"Mr. Yogendra  Singh",
"Mr.   ",
"Mr. rachit  Gupta",
"Mr. Raman  Malhotra",
"Mr. American  Jain",
"Mr. aakash  gandhi",
"Mr. Mohd  Ali",
"Mr.   ",
"Mr. Arpit  Gupta",
"Mr. nitish  Kumar",
"Mr. Drupad  Jain",
"Mr. Ashish  Moza",
"Mr. Rajat  Jain",
"Mr. Shan  Kumar",
"Mrs. Mahinder  Kapoor",
"Mr.   ",
"Ms. megha  priya",
"Ms. lata  gupta",
"Mr. Test  Patientb",
"Dr. Himanshu Kumar Parashar",
"Mr. Gfh  Gfh",
"Mr.   ",
"Mr. ZX  Zxc",
"Ms. manvi  mehra",
"Mr. ramesh  Babu",
"Mr. Dushyant Kumar Sharma",
"Mr. Asd Asd Asd",
"Mr. Rachit  Gupta",
"Mr. Sachin  Kumar",
"Mr. Rakesh  Sharma",
"Mr.   ",
"Mr. Amit  Kumar",
"Mr. Ram Kumar Naresh",
"Mr.   ",
"Mr. John Kumar Cena",
"Mr. sahd  df",
"Mr. Asdfasd Asf Qweqwe",
"Mr. Sanjeev  Kumar",
"Mr. Geeta  Sharma",
"Mr. Ram  Kumar",
"Mr. Sunil  Rai",
"Mr. atul  gupta",
"Mr.   ",
"Mr. martin  luther",
"Mr. Asd Qwe Qwe",
"Mr. Test  Kumar",
"Mr. Shrey  Bhalla",
"Miss Neha  Jain",
"Ms. Anushka  Sharma",
"Miss Jaysheer  Jain",
"Mr. Fhdf Bgjgh ",
"Mr. Deepak  Gupta",
"Mr. Test  Patienta",
"Mr. Subh  Karmakar",
"Mr. Mohan  Kumar",
"Mr. apoorv  sharma",
"Mr. ankit  rathor",
"Mr. tarun  gupta",
"Mr. Mohd  Rizwan",
"Mr. hello  sd",
"Mr. Amit  Gupta",
"Mr. Dushyant 2nd  Sharma",
"Mr. anshul  aggarwal",
"Mr. Asd Asd Asd",
"Mr. Jitesh  Kumar",
"Mr. Hodor Hodor Hodor",
"Mr. Abhinav  Goel",
"Mr. Testz Z Patietn",
"Mr. Mohit  Dani",
"Mr.   ",
"Mr. Jinender  Jain",
"Mr. aditya  rai",
"Mr. Ankit Kjhk Loonia",
"Master Himanshu  Kumar",
"Mr. mohan  bhargav",
"Mr. Mohan  Kumar",
"Mr. Satypal  Kapoor",
"Mr. Mohit  Raina",
"Mr. Asd Qwe Qwe",
"Mr. test  test",
"Mr. Garima  Gupta",
"Mr. Mayank  Jain",
"Mr. Test  C"

) 此数据加载在自定义表中,如....

 table_SearchPatient=[[UITableView alloc]initWithFrame:CGRectMake(130,40,170,250)style:UITableViewStylePlain];
table_SearchPatient.delegate=self;
table_SearchPatient.dataSource=self;
table_SearchPatient.layer.borderWidth = 2.0;
table_SearchPatient.layer.borderColor = [UIColor grayColor].CGColor;
[self.view addSubview:table_SearchPatient];

此表成功加载所有数据以及如何搜索所需名称使用自定义文本字段....请解决问题..

4 个答案:

答案 0 :(得分:2)

实施UITextFieldDelegate和方法

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;

在此方法中,当您在文本字段上书写时,可以搜索阵列中的文本并填充已过滤的数组。然后使用reloadData重新加载表。

答案 1 :(得分:0)

试试这个::

[[NSNotificationCenter defaultCenter] 
                addObserver:self 
                   selector:@selector(anyMethod:)
                       name:UITextFieldTextDidChangeNotification 
                     object:textField1];

on change text Method(此处为“anyMethod:”)获取文本字段文本并获取与textfield文本匹配的记录并将其保存在另一个数组中。然后使用该筛选的数组加载数据。所以它看起来像tableview中的文本过滤器。

希望它能帮助!!

答案 2 :(得分:0)

首先,您必须添加观察者以获取文本更改通知。

[[NSNotificationCenter defaultCenter] 
            addObserver:self 
               selector:@selector(textFieldDidChangeText:)
                   name:UITextFieldTextDidChangeNotification 
                 object:textField];

然后实现给定的文本更改通知方法

-(void)textFieldDidChangeText:(id)sender {

NSString *text = [sender text];

tableViewDataSrcArray = [dataSrcArray filterUsingPredicate:[NSPredicate predicateWithFormat:@"SELF contains[cd] %@", text]];
[tableView reloadData];
}

假设'dataSrcArray'是您的实际列表,'tableViewDataSrcArray'是您的表视图数据源数组。

希望这会对你有所帮助:)。

答案 3 :(得分:0)

如果您使用搜索栏,请从表格数组创建临时数组。并使用以下方法

-(void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)text
{
    if(text.length == 0)
    {
        isFiltered = FALSE;
        tableDataArray  = tempArray;
    }
    else
    {
        isFiltered = TRUE;
        NSMutableArray *searchArray = [NSMutableArray arrayWithArray:friendsArray];
        NSPredicate *predicate  = [NSPredicate predicateWithFormat:@"name contains[c] %@",text];
        tableDataArray  =[NSMutableArray arrayWithArray:[searchArray filteredArrayUsingPredicate:predicate]];

    }

    [self.tableView reloadData];
    [searchBar becomeFirstResponder];

}