2012-01-19 2 views
1

검색 표시 줄에이 코드를 사용하고 있지만 결과가 표시되지 않습니다.검색 표시 줄이 작동하지 않습니다 iPhone Xcode 4.2

filteredListitems = [[NSMutableArray alloc] initWithArray:listVehicles]; 

검색 창 방법 :있는 viewDidLoad 방법에서

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { 

if ([searchText length] == 0) { 
    [filteredListitems removeAllObjects]; 
    [filteredListitems addObjectsFromArray:listVehicles]; 
}else { 

    [filteredListitems removeAllObjects]; 
    for (NSString * string in listVehicles) { 
     NSRange r = [string rangeOfString:searchText options:NSCaseInsensitiveSearch]; 
     if (r.location != NSNotFound) { 
      [filteredListitems addObject:string]; 
     } 
    } 
}  
[listTable reloadData];} 

-(void)searchBarSearchButtonClicked:(UISearchBar *)aSearchBar { 

[searchBar resignFirstResponder]; 
} 

각 셀에 대한 코드는 다음

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

static NSString *CellIdentifier = @"vlCell"; 

VehicleListCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if (cell == nil) { 

    NSLog(@"Cell Created"); 

    NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:@"VehicleListCell" owner:nil options:nil]; 

    for (id currentObject in nibObjects) { 

     if ([currentObject isKindOfClass:[VehicleListCell class]]) { 

      cell = (VehicleListCell *)currentObject; 
     } 
    } 

    UILongPressGestureRecognizer *pressRecongnizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(tableCellPressed:)]; 
    pressRecongnizer.minimumPressDuration = 0.5f; 
    [cell addGestureRecognizer:pressRecongnizer]; 
    [pressRecongnizer release]; 

} 

cell.textLabel.font = [UIFont systemFontOfSize:10]; 

    [[cell ignition] setImage:[UIImage imageNamed:@"ignition.png"]]; 
    [[cell direction] setImage:[UIImage imageNamed:@"south.png"]]; 
    NSString *cellValue = [filteredListitems objectAtIndex:indexPath.row]; 
    cell.licPlate.text = cellValue; 

return cell; 

}는

I 검색 바 시도했다 디스플레이 컨트롤러, w 잘 작동하지만 필터링 된 검색 결과에 대한 자신의 테이블보기를 보여주는 것을 내가 같은 테이블의 다른 열을 표시하는 사용자 정의 셀을 사용하고있는 동안 문제였다 같이 나도 같은보기를 원하는

enter image description here

을 위와 같이 검색하는 동안 또는 검색 후 나는이보기를 얻을 동안 검색

를 완료 한 후

enter image description here

tablevi의 차이를보기 검색 헤더가 사라지면 ews가 표시됩니다. 누군가가 디스플레이 컨트롤러가없는 경우에만 세이코 바를 사용하도록 제안했습니다. 나는 내 응용 프로그램에서 검색보기 컨트롤러 사용하고이 문제

+0

NSLog 배열의 filteredListitems을. – Hiren

+0

이 선택되어 있으면 검색 버튼이 작동하지 않고 filteredListItems 배열이 listVehicles의 모든 항목을 표시합니다. –

+0

검색 막대를보기에 추가하는 방법은 무엇입니까? – Hiren

답변

2

당신은 검색 디스플레이 컨트롤러를 사용해야 있도록

저를 인도 해주십시오.

키는 테이블 뷰 데이터 소스 & 대리자 메서드를 호출하지만 테이블 뷰를 첫 번째 매개 변수로 전달한다는 것이 중요합니다. 예를 들어

(당신은 yourTableView라는 이름의 인스턴스 변수에 테이블 뷰에 대한 참조를 저장하는 경우) : if 문에서

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    if (tableView == yourTableView) { 
     return [listVehicles count]; 
    } else { // handle search results table view 
     return [filteredListItems count]; 
    } 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSString *cellValue = nil; 

    if (tableView == yourTableView) { 
     cellValue = [listVehicles objectAtIndex:indexPath.row]; 
    } else { // handle search results table view 
     cellValue = [filteredListItems objectAtIndex:indexPath.row]; 
    } 

    static NSString *CellIdentifier = @"vlCell"; 

    VehicleListCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 

    NSLog(@"Cell Created"); 

    NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:@"VehicleListCell" owner:nil options:nil]; 

     for (id currentObject in nibObjects) { 
      if ([currentObject isKindOfClass:[VehicleListCell class]]) { 
       cell = (VehicleListCell *)currentObject; 
      } 
     } 

     UILongPressGestureRecognizer *pressRecongnizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(tableCellPressed:)]; 
     pressRecongnizer.minimumPressDuration = 0.5f; 
     [cell addGestureRecognizer:pressRecongnizer]; 
     [pressRecongnizer release]; 
    } 

    cell.textLabel.font = [UIFont systemFontOfSize:10]; 

    [[cell ignition] setImage:[UIImage imageNamed:@"ignition.png"]]; 
    [[cell direction] setImage:[UIImage imageNamed:@"south.png"]]; 

    cell.licPlate.text = cellValue; 

    return cell; 
} 
+0

안녕하세요 gerry3 고맙습니다. .... 원하는대로 검색 표보기를 가져 오는 중입니다. , 테이블 뷰 및 검색 막대 컨트롤러와 같은 유용한 정보는 애플리케이션에서 주로 사용됩니다. 질문이 하나 더 있습니다. 검색 한 내 자신의 tableview에 대한 코드가 tableview와 동일한 결과를 제공하지만 A, B, C 및 D를 사용한 헤더는 어떻게 표시 할 수 있습니까? 검색 테이블보기 –

+0

테이블보기에서 헤더를 어떻게 보이고 있습니까? – gerry3

+0

헤더를 표시하기 위해 UIView에 라벨을 사용합니다. –

0

를 해결하는 것이 대리자 메서드를 구현하는

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString { 
if(self.searchDisplayController.searchBar.text.length>0) { 
    self.isSearching=YES; 

    NSString *strSearchText = self.searchDisplayController.searchBar.text; 
    NSMutableArray *ar=[NSMutableArray array]; 
    // correctly working ! Thanx for watching video ! 
    for (int i=0; i<self.arOriginal.count; i++) { 
     NSString *strData = [self.arOriginal objectAtIndex:i]; 
     NSRange rng = [strData rangeOfString:strSearchText options:NSCaseInsensitivePredicateOption]; 
     if(rng.length > 0){ 
      [ar addObject:strData]; 
     } 
    } 
    self.arFiltered=[NSArray arrayWithArray:ar]; 
} else { 
    self.isSearching=NO; 
} 
return YES; 
} 
+0

개 이상의 hiren443을 사용하여 디버깅을 시도했지만 검색 결과에 대한 테이블보기 문제는 어떻게 해결할 수 있습니까? –

관련 문제