2010-08-21 9 views
0

내 iPhone 응용 프로그램에는 검색 기능이 구현 된 UITable보기가 있습니다. 표의 값은 A-Z 섹션으로 그룹화됩니다. 사용자가 테이블의 특정 셀을 탭할 때마다 해당 특정 사용자의 모든 값을 제공하는 상세보기 컨트롤러를로드합니다. 이제 내 문제는 특정 연락처를 검색하고 특정 사용자를 클릭하여 세부 정보보기를 확인하면 문자 A로 시작하는 연락처를 항상 반환합니다. 따라서이 검색 기능을 구현하는 방법에 대해 의문의 여지가 있습니다. 내가 도청 한 연락처의 이름을 얻는 방법이 있습니까?이 스크린 샷을 확인하십시오. 예를 들어 문자 'B'로 시작하는 일부 연락처를 검색하고 해당 연락처를 누르면 문자로 시작하는 연락처의 상세보기가로드됩니다 '에이'. 나는 데이터베이스에서 모든 값을 얻는다.UITableView 검색 기능을 구현하는 방법

내가 여기에 쓴 방법에 코드 : 당신은 alt text

이 코드는 ... 나 좀 도와 주실 래요

나는 데이터베이스에서 모든 연락처를 얻기과에 할당하고 배열 접촉. 그런 다음 알파벳에 따라 모든 연락처를 그룹화하고 A부터 Z까지의 키가있는 사전과 모든 문자를이 문자로 시작하는 연락처 이름으로 그룹화합니다. 이제 특정 연락처를 검색 할 때 그의 이름은 검색 창에서 A, B 또는 Z로 시작될 수 있습니다. 예를 들어 문자 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... 일부 코드를 게시했습니다. 제발 ..이 일을 마무리해야합니다. 그것은 나에게 매우 나쁜 말인데, 어떻게해야할지 모르겠습니다. 나는 초보자입니다 ... 제발 도와주세요 ... – racharambola

+0

Greelmo .. 코드를 확인하고 접근 절차를 알려주세요. 서면으로 문제가 있습니다. 검색 기능을위한 SelectRowAtIndexPath – racharambola

+0

시간이 없습니다. 당신을 위해 코드를 작성하십시오. tableviews에서 검색 창을 사용하는 방법에 대한 자습서를 찾아보십시오. 여기에 힌트가 있습니다. didSelectRowAtIndexPath가 tableView를 제공하기 때문에 2 개의 tableviews가 필요하며 어떤 테이블 뷰가 사용되는지 확인할 수 있습니다. – DexterW

관련 문제