如何实现UITableView搜索功能
我的iPhone应用程序有一个UITable视图,其中具有搜索功能。表中的值从A-Z分组为几个部分。每当用户点击表格中的特定单元格时,它会加载一个详细视图控制器,该控制器将显示该特定用户的所有值。现在我的问题是,每当我搜索一些联系人并点击特定用户来检查他的详细视图时,它总是返回一个以字母A开头的联系人。所以,我的疑问是如何实现此搜索功能。有没有办法获得我点击的联系人的名字..请检查此截图..例如,如果我搜索以字母'B'开头的一些联系人并点击该联系人,它将加载以字母开头的联系人的详细视图'一个'。我从数据库中获取所有值。能否请你帮我...... 如何实现UITableView搜索功能
这是代码:
我写到这里是一个方法的代码:
我让所有从数据库中的联系人和分配到阵列联系人。然后,我按照字母对所有联系人进行分组,并将所有联系人分组到字典中,其中键为A-Z,并将值作为以这些字母开头的联系人的名称。现在,当我搜索特定的联系人时,他的名字可以在搜索栏中以A,B或Z..so开头,当我搜索特定的联系人时,例如以字母Z开头的联系人,在这种情况下,它给出一个有A的人。我希望这个改变,以便每当我点击一个特定的联系人时,它应该加载它的细节。我无法弄清楚如何做到这一点..
contacts = [[db getContacts:@"Contacts"] componentsSeparatedByString:@","];
[db cleanup];
NSMutableArray *tempArray = [[NSMutableArray alloc] init];
NSString *charString;
for (int i=65; i<91; i++) {
charString = [NSString stringWithFormat:@"%c",(char *)i];
[tempArray addObject:charString];
}
[charString release];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
for (int i=0; i<[tempArray count]; i++) {
NSMutableArray *contactsByIndex = [[[NSMutableArray alloc] init]autorelease];
NSString *tempChar = [tempArray objectAtIndex:i];
for (int j=0; j<[contacts count]-1; j++)
{
NSString *test = [contacts objectAtIndex:j];
NSString *tempString = [test substringToIndex:1];
if ([tempString isEqualToString:tempChar]) {
[contactsByIndex addObject:[contacts objectAtIndex:j]];
}
}
[dict setObject:contactsByIndex forKey:[tempArray objectAtIndex:i]];
}
self.contactNames = dict;
NSArray *array = [[contactNames allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
self.contactKeys = array;
[dict release];
[tempArray release];
//---display the searchbar---
self.tableView.tableHeaderView = searchBar;
searchBar.autocorrectionType = UITextAutocorrectionTypeYes;
listOfContacts = [[NSMutableArray alloc] init];
for (NSString *key in array)
{
NSArray *contactsArray = [contactNames objectForKey:key];
for (NSString *name in contactsArray) {
[listOfContacts addObject:name];
}
}
- (void) searchContactsTableView {
//---clears the search result---
[searchResult removeAllObjects];
for (NSString *str in listOfContacts) {
NSRange titleResultsRange = [str rangeOfString:searchBar.text options:NSCaseInsensitiveSearch];
if (titleResultsRange.length > 0)
[searchResult addObject:str];
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic may go here. Create and push another view controller.
NSString *selectedRow=nil;
if (isSearchOn) {
selectedRow=[searchResult objectAtIndex:indexPath.row];
}
DetailViewController *detailViewController;
int section_index=[indexPath indexAtPosition:[indexPath length]-2];
int sugarid_Index = [indexPath indexAtPosition: [indexPath length]-1];
NSString* sectionName=[contactKeys objectAtIndex:section_index];
NSLog(@"%@",sectionName);
//This is a method which gets the details of a particular contact based on the section and the row selected..Contacts is the table name
NSString *getContact=[db getId:@"Contacts" bySection:sectionName andIndex:sugarid_Index];
id=[db getContact:@"Contacts" id:getContact];
// Pass the selected object to the new view controller.
detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
detailViewController.eachContact=contactForSugarId;
[self.navigationController pushViewController:detailViewController animated:YES];
}
当我搜索联系人,应该在数据库中搜索名称,应返回其详细信息。有没有办法做到这一点..请让我知道或有办法获取单元格的名称,即联系人名称,以便我可以在我的数据库方法中使用它来检索我选择的联系人的详细信息。
这听起来好像是在搜索后看到错误的联系人数组。你需要有两个数组。一个与所有的联系人,一个与过滤的联系人。在搜索时,将所有结果按顺序排列在过滤列表中,并从中抽取详细信息。
这是否有意义?
如果不是,请尝试发布一些代码并解释您的结构。
感谢您的回复Greelmo..I发布了一些代码..请看看它..我需要完成这项任务,因为它是非常隐瞒我,我无法弄清楚如何做到这一点我是一个新手...请帮助我... – racharambola 2010-08-22 03:45:59
Greelmo ..你可以检查代码,并告诉我的过程来接近它..我有问题在编写didSelectRowAtIndexPath搜索功能 – racharambola 2010-08-22 17:33:29
我没有时间为你写代码。只要查看关于在桌面上使用搜索栏的一些教程。 这里有个提示:你需要2个tableviews,你可以检查使用哪一个,因为didSelectRowAtIndexPath给你tableView。 – DexterW 2010-08-23 13:40:58