1

저는 수십 개의 서로 다른 블로그를 읽고 몇 시간을 보냈습니다. 다른 사람들의 블로그는 여기에 있습니다.하지만 저는 막 다른 골목에 있습니다.UISearchDisplayController - 구현하고 작동시키는 방법

제 질문은이 문제를 어떻게 해결합니까? 나는 IB와 코드를 통해 그것을 시도했다.

IB에서 시도했을 때 테이블 뷰 (UITableViewController)로 드래그하고 헤더가 속한 테이블에 "검색"되어도 검색 막대가 표시되지 않습니다. 하지만 검색 창은 나타나지 않습니다. (하위 질문입니다. 왜 그런지 아십니까?)

그래서 코드를 설정해 보았습니다. 내가 할당 된, 대리인 및 데이터 원본을 할당 할 수 있지만 절대로 "결과가 없습니다"라는 테이블보기 얻을. 막대가 나타 났고 입력 할 수 있지만 결과가 있어야한다는 것을 알고 있더라도 항상 빈 테이블 만 표시합니다. 그 셀이 "결과 없음"텍스트가 표시되지 않아서 올바른 테이블을 보여주지 못하는 것은 의심 스럽습니다. 실패의

- (void)viewDidLoad { 

    [super viewDidLoad]; 

    names = [[NSMutableArray alloc] initWithObjects:@"Michael", @"Dwight", @"Jim", @"Andy", @"Ryan", @"Creed", @"Darryl", @"Kevin", @"Oscar", @"Gabe", nil]; 
    results = [[NSMutableArray alloc] init]; 

    UISearchBar *sb = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; 
    sb.tintColor = [UIColor greenColor]; 
    searchTable.tableHeaderView = sb; 

    UISearchDisplayController *sdc = [[UISearchDisplayController alloc] initWithSearchBar:sb contentsController:self]; 
    [self setSearchDisplayController:sdc]; 
    [sdc setDelegate:self]; 
    [sdc setSearchResultsDataSource:self];} 


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    if (tableView == self.searchTable) { 
     // normal table view population 
     return [names count]; 
    } 
    if(tableView == self.searchDisplayController.searchResultsTableView){ 
     // search view population 
     return [results count]; 
    } 
    return 0; 
} 

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{  
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     cell.selectionStyle = UITableViewCellSelectionStyleGray; 
    } 

    if (tableView == self.searchTable) { 
     // normal table view population 
     cell.textLabel.text = [names objectAtIndex:indexPath.row]; 
    } 
    if(tableView == self.searchDisplayController.searchResultsTableView){ 
     // search view population 
    } 

    cell.textLabel.textColor = [UIColor blueColor]; 

    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (tableView == self.searchTable) { 
     // normal table view population 
    } 
    if(tableView == self.searchDisplayController.searchResultsTableView){ 
     // search view population 
    } 
} 


- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString 
{ 
    // Return YES to cause the search result table view to be reloaded. 
    return YES; 
} 

- (void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller 
{ 
    [results removeAllObjects]; // First clear the filtered array. 

    /* 
    Search the main list for products whose type matches the scope (if selected) and whose name matches searchText; add items that match to the filtered array. 
    */ 
    for (NSString *str in names) 
    { 
     NSComparisonResult result = [str compare:controller.searchBar.text options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [controller.searchBar.text length])]; 
     if (result == NSOrderedSame) 
     { 
      [results addObject:str]; 
     } 
    } 
} 

Any help would be so very much appreciated. I would add a bounty, but as you can see, I've got "Newb" written on my forehead still :). 
+0

일부 코드를 보지 않으면 무엇이 잘못 될지 알기가 어렵습니다. – conmulligan

답변

0

몇 가능한 점 I는 다음을 참조하십시오

1) 당신은 그것의 .H 파일에 대리인으로 당신의 ViewController를 설정하는? 예 :

@interface myViewController : UIViewController <UISearchBarDelegate,UISearchDisplayDelegate,UITableViewDataSource,UITableViewDelegate>{ 

2)이 당신의 전체 코드, 또는 단지 관련 조각의 경우 내가 말할 수는 없지만, 전체 코드의 경우 모든 데이터가 보여주는 볼 전에 몇 구멍이 있습니다. 결과를 표시하고 cellForRowAtIndexPath 메소드에서 두 번째 if 문을 채우기 위해 테이블 ​​뷰를 어딘가에 추가해야합니다.

Apple에서 TableSearch 샘플 코드를 확인하는 것이 좋습니다. 올바른 방향으로 이동해야합니다.

+0

나는 그 샘플을 따라 왔고 어떻게 작동 하는지를 알 수 없다. 나는 IB에서의 나의 관계를 보았다. 그들은 완전히 똑같다. 나는 그 대표들을 거기에 두었습니다. 그래서 결과에서 테이블을 만들어야 만합니다. searchController는 그렇게하지 않습니다. – sasquatch

+0

예제 코드에서 검색 결과를 테이블에 넣지 않는 것처럼 보입니다. if (tableView == self.searchDisplayController.searchResultsTableView) { cell.textLabel.text = [results objectAtIndex : indexPath.row]; }' – Sam

+1

그래서이 테이블을 작성해야한다면 searchDisplayController를 사용할 때의 요점은 무엇입니까? 나는 모든 것을해야만한다면, 어떤 이점이 있는가, 멋진 애니메이션일까요? – sasquatch

관련 문제