2011-12-04 3 views
0

내 테이블보기에 검색 막대를 설정 했으므로 textLabels (기본 텍스트) 및 detailTextLabels (기본 텍스트)를 검색하면서 검색 범위를 좁힐 때 기본 레이블과 세부 레이블 간의 관계를 유지합니다. 현재 주요 라벨 만 검색 할 수 있지만 세부 라벨은 관련 주요 라벨과 함께 유지되지 않습니다. 기본 레이블과 세부 레이블에 대한 문자열을 포함하는 두 개의 배열이 있습니다. 여기에 몇 가지 코드가 있습니다. 섹션UITableView 검색 막대 : textLabels 및 detailTextLabels (iOS 5.0, xcode 4.2)를 모두 검색하고 싶습니다.

메인 라벨 (groceryStores)를 운반하는 배열 및 세부 라벨 (groceryStoreAddresses)의 초기화뿐만 아니라 탐색 어레이

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.groceryStores = [NSArray arrayWithObjects:@"Safeway",@"Grocery Store 1",@"Grocery Store 2",@"Grocery Store 3",@"Grocery Store 4",@"Grocery Store 5",@"Grocery Store 6",@"Grocery Store 7",nil]; 

    self.groceryStoreAddresses = [NSArray arrayWithObjects:@"1444 Shattuck Place, Berkeley, CA 94709",@"Address 1",@"Address 2",@"Address 3",@"Address 4",@"Address 5",@"Address 6",@"Address 7",nil]; 

    self.searchGroceryStores = [NSMutableArray arrayWithCapacity:[self.groceryStores count]]; 


} 

검색 방법

- (BOOL) searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString { 
    [self.searchGroceryStores removeAllObjects] ; 

    for (NSString *groceryStore in self.groceryStores) { 



     NSRange result = [groceryStore rangeOfString:searchString options: 
          NSCaseInsensitiveSearch]; 
     if (result.location != NSNotFound) 
      [self.searchGroceryStores addObject:groceryStore]; 

    } 

    return YES; 
} 

행 검색 기능을 위해 조정 됨

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 

    if (tableView == self.searchDisplayController.searchResultsTableView) { 
     return [self.searchGroceryStores count]; 
    } 
    else { 

    return [self.groceryStores count]; 
    } 
} 

cellForRowAtIndexPath 검색 기능을 위해 조정 됨

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"UITableViewCellAccessoryDisclosureIndicator"]; 
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;   

    if (tableView == self.searchDisplayController.searchResultsTableView) { 
     cell.textLabel.text = [self.searchGroceryStores objectAtIndex:indexPath.row]; 
     cell.detailTextLabel.text = [self.groceryStoreAddresses objectAtIndex:indexPath.row]; 
    } 
    else { 
     cell.textLabel.text = [self.groceryStores objectAtIndex:indexPath.row]; 
     cell.detailTextLabel.text = [self.groceryStoreAddresses objectAtIndex:indexPath.row]; 
    } 

    // Configure the cell... 

    return cell; 
} 

가능한 경우 도움을 받으십시오. 시간 내 주셔서 고맙습니다. 고맙습니다.

답변

1

searchGroceryStores 어레이와 병렬로 searchGroceryStoreAddresses 어레이를 유지해야합니다. 예 :

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString { 
    [self.searchGroceryStores removeAllObjects]; 
    [self.searchGroceryStoreAddresses removeAllObjects]; 
    for (int i = 0, c = self.groceryStores.count; i < c; ++i) { 
     NSString *groceryStore = [self.groceryStores objectAtIndex:i]; 
     NSString *address = [self.groceryStoreAddresses objectAtIndex:i]; 
     NSRange result = [groceryStore rangeOfString:searchString options: 
          NSCaseInsensitiveSearch]; 
     if (result.location != NSNotFound) { 
      [self.searchGroceryStores addObject:groceryStore]; 
      [self.searchGroceryStoreAddresses addObject:address]; 
     } 
    } 
    return YES; 
} 
+0

searchGroceryStoreAddresses 배열을 만들었지 만 searchDisplayController 메서드에 어떻게 구현합니까? 매번 && 또는 ||를 추가하려고합니다. 문을 for 루프 인수에 지정하면 xcode는 오류를 발생시킵니다. – user1072337

+0

두 배열을 서로 병렬로 유지하는 방법을 찾는 데 여전히 문제가 있습니다. 두 배열의 동일한 색인에있는 객체가 서로 동기화되어 정확한 세부 레이블이 해당 텍스트 레이블과 함께 나타나도록하려면 어떻게해야합니까? – user1072337

+0

두 배열을 항상 함께 업데이트해야합니다. –

관련 문제