1

你好朋友在我的一项活动中,我创建了自定义文本字段,并且我有一个包含 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

4 回答 4

2

实现 UITextFieldDelegate 和方法

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

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

于 2013-09-05T07:15:09.130 回答
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”是您的表视图数据源数组。

希望对你有帮助 :)

于 2013-09-05T07:50:22.307 回答
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];

}
于 2013-09-05T08:15:13.670 回答
0

试试这个方法::

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

更改文本方法(此处为“anyMethod:”)获取文本字段文本并获取与文本字段文本匹配的记录并将其保存在另一个数组中。然后使用该过滤后的数组加载数据。使它看起来像表格视图中的文本过滤器。

希望能帮助到你!!

于 2013-09-05T07:18:53.427 回答