2013-06-28 4 views
2

좋은 날은 검색 항목를 표시합니다. 아래 다음과 같이reloadData

내 코드입니다 : 문제는, 내가 내 있는 viewDidLoadGETSEARCH 함수를 호출 할 때, 그것을 실행하고 콜백 TITLEITEMSRETURNED을 수행합니다. 그리고 tableview는을 올바르게 다시로드합니다.

그러나 검색 창을 사용하고 GETSEARCH을 수행하면됩니다. 대리자가 호출되고 데이터가 배열로 올바르게로드되지만 tableView는 업데이트되지 않습니다.

그러나 회색 십자 버튼을 누르면 갑자기 테이블이 업데이트됩니다. 뭐라 구요?

-(void)TitleItemsReturned:(NSArray*)titleItems{ 
    for(TitleItem* titleItem in titleItems){ 
     // NSLog(@"TITLE: %@ ISBN: %@",titleItem.Title,titleItem.ISBN); 
     [searchResults addObject:titleItem]; 
    } 
    [self.tableView reloadData]; 
} 

- (void)viewDidLoad 
{ 
    NSLog(@"RUN"); 
    networkLayer=[[NLBNetworkLayer alloc]init]; 
    searchResults=[[NSMutableArray alloc]initWithCapacity:500]; 
// [networkLayer getBookSearch:TITLE term:@"Inferno"]; 
    [super viewDidLoad]; 
} 

-(void)viewDidAppear:(BOOL)animated{ 
    [networkLayer setDelegate:(id)self]; 
} 


#pragma mark - Table view data source 

-(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]; 
    } 
    TitleItem *titleItem = nil; 
    titleItem = [searchResults objectAtIndex:indexPath.row]; 
// Configure the cell 
    cell.textLabel.text = titleItem.Title; 
    NSLog(@"called %@",titleItem.Title); 
    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; 
    return cell; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    NSLog(@"count %d",[searchResults count]); 
    return [searchResults count]; 
} 

#pragma mark - UISearchDisplayController Delegate Methods 
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller  shouldReloadTableForSearchString:(NSString *)searchString { 
    return YES; 
} 

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{ 
    //[networkLayer getBookSearch:TITLE term:searchBar.text]; 
    [networkLayer getBookSearch:TITLE term:@"Inferno"]; 
} 

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{ 
    NSLog(@"all removed"); 
    [searchResults removeAllObjects]; 
    [self.tableView reloadData]; 
} 
+0

테이블 리로드가 완료된 후에 검색 하시겠습니까? – Ganapathy

+0

networklayer에서 내 getBookSearch 메소드를 보여줄 수 있습니까? – wesley

답변

4

그렇지 않으면 당신은 문제가있을 수 있습니다, 당신은 메인 스레드에서 reloadData 메시지를 전송되어 있는지 확인합니다. TitleItemsReturned 메소드는 메인 스레드 (예를 들어 networkLayer 객체에 의해 구현 된 NSURLConnectionDelegate 메소드 또는 유사한 델리게이트 메소드의 백그라운드 스레드)로부터 호출되지 않을 수있다.

dispatch_async(dispatch_get_main_queue(), ^{ 
    [self.tableView reloadData]; 
}); 

그 방법은 UI 이벤트에서 (주 스레드에서 실행되기 때문에 searchBarCancelButtonClicked 방법이 작동 : TitleItemsReturned 실제로 메인 스레드에서 실행되지 않는

경우 TitleItemsReturned의 내부 작업을 수행 할 수 있습니다).