2013-10-31 3 views
0

테이블 뷰의 검색 막대를 추가해야합니다. "환자"라는 개체 내부에 실제로 저장된 "이름"이 있습니다. "환자"개체의 배열이 있습니다. 검색 막대를 설정하여 이름을 검색하십시오. 그러나 NSPredicate를 사용하여 이름을 검색하는 방법은 무엇입니까?테이블 뷰 검색 막대

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
     static NSString* CellId= @"cell"; 
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellId]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellId] ; 
    } 
    cell.accessoryType = UITableViewCellAccessoryNone; 

    ObjPatient = [allPatientDetails objectAtIndex:indexPath.row]; 
    cell.textLabel.text =[NSString stringWithFormat:@"%@ %@",ObjPatient.firstName,ObjPatient.lastName]; 
    return cell; 

} 

위의 표에있는 이름을 표시하는 코드입니다. 모든 제안을 환영합니다, 감사합니다

+0

http://www.appcoda.com/how-to-add-search-bar-uitableview/ –

+0

감사합니다. 문제는 NSPredicate를 사용하여 object의 속성 인 "name"을 만드는 방법입니다. 이러한 객체의 배열이 있습니다. –

답변

5

검색 기능을 구현하는 객체의 속성입니다 -

https://developer.apple.com/library/ios/documentation/uikit/reference/UISearchBar_Class/Reference.html https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSPredicate_Class/Reference/NSPredicate.html

그런 다음 확인 코드에서 다음과 같이 변경 -

.h 파일 선언 배열에 -

NSArray *filteredArray; 

은에서 - (무효)의 viewDidLoad 방법 - 다음

filteredArray = allPatientDetails; 

구현 UISearchBar 대리자

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText 
{ 
    NSPredicate *predicate = [NSPredicate 
          predicateWithFormat:@"SELF.firstName contains[c] %@", 
          searchText]; 
    NSArray *filteredArray = [allPatientDetails filteredArrayUsingPredicate:predicate]; 
    [tableView reloadData]; 
} 

을 방법 - 당신의 기존 방법 변경 -

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

    static NSString* identifier= @"cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; 

    if (!cell) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] ; 

    } 
    cell.accessoryType = UITableViewCellAccessoryNone; 

    ObjPatient = [filteredArray objectAtIndex:indexPath.row]; 
    cell.textLabel.text =[NSString stringWithFormat:@"%@ %@",ObjPatient.firstName,ObjPatient.lastName]; 
    return cell; 
} 
+0

대단히 감사합니다 :) –

0

잘 정의 된 개체와 nspredicate이 같은 할 수 있습니다.

NSPredicate *resultPredicate = [NSPredicate 
           predicateWithFormat:@"SELF.firstName contains[c] %@", 
           searchText]; 


self.searchedMemberNameResults = [self.listedMembers filteredArrayUsingPredicate:resultPredicate]; 

firstName을 먼저 아래 링크로 볼 걸릴

0

아래에서 항목 검색을위한 코드를 찾을 수 있습니다. 스토리 보드의 라벨에 그것은 동적으로 검색합니다. 나는 이것을 테스트했고 효과가있다.

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

     NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"job_id contains[c] %@",searchText]; 

     searchResultArray = [responceArray filteredArrayUsingPredicate:resultPredicate]; 

     [jobsTable reloadData]; 
    } 
관련 문제