2010-08-21 235 views
0

我的iPhone應用程序有一個UITable視圖,其中具有搜索功能。表中的值從A-Z分組爲幾個部分。每當用戶點擊表格中的特定單元格時,它會加載一個詳細視圖控制器,該控制器將顯示該特定用戶的所有值。現在我的問題是,每當我搜索一些聯繫人並點擊特定用戶來檢查他的詳細視圖時,它總是返回一個以字母A開頭的聯繫人。所以,我的疑問是如何實現此搜索功能。有沒有辦法獲得我點擊的聯繫人的名字..請檢查此截圖..例如,如果我搜索以字母'B'開頭的一些聯繫人並點擊該聯繫人,它將加載以字母開頭的聯繫人的詳細視圖'一個'。我從數據庫中獲取所有值。能否請你幫我...... alt text如何實現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]; 

} 

當我搜索聯繫人,應該在數據庫中搜索名稱,應返回其詳細信息。有沒有辦法做到這一點..請讓我知道或有辦法獲取單元格的名稱,即聯繫人名稱,以便我可以在我的數據庫方法中使用它來檢索我選擇的聯繫人的詳細信息。

回答

0

這聽起來好像是在搜索後看到錯誤的聯繫人數組。你需要有兩個數組。一個與所有的聯繫人,一個與過濾的聯繫人。在搜索時,將所有結果按順序排列在過濾列表中,並從中抽取詳細信息。

這是否有意義?

如果不是,請嘗試發佈一些代碼並解釋您的結構。

+0

感謝您的回覆Greelmo..I發佈了一些代碼..請看看它..我需要完成這項任務,因爲它是非常隱瞞我,我無法弄清楚如何做到這一點我是一個新手...請幫助我... – racharambola 2010-08-22 03:45:59

+0

Greelmo ..你可以檢查代碼,並告訴我的過程來接近它..我有問題在編寫didSelectRowAtIndexPath搜索功能 – racharambola 2010-08-22 17:33:29

+0

我沒有時間爲你寫代碼。只要查看關於在桌面上使用搜索欄的一些教程。 這裏有個提示:你需要2個tableviews,你可以檢查使用哪一個,因爲didSelectRowAtIndexPath給你tableView。 – DexterW 2010-08-23 13:40:58

相關問題