UISearchDisplayController를 사용하여 서버에서 검색하는 일부 데이터를 기반으로 사용자 지정 셀이있는 테이블을 표시 할 수 있습니다.사용자 지정 UITableViewCell의 속성이 UISearchDisplayController의 테이블에 대해 초기화되지 않았습니다.
먼저 UIViewController 내에서 UISearchDisplayController를 설정합니다.
self.searchController = [[UISearchDisplayController alloc]
initWithSearchBar:self.mySearchBar contentsController:self];
self.searchController.delegate = self;
self.searchController.searchResultsDataSource = self;
self.searchController.searchResultsDelegate = self;
내 UIViewController에 또한 UISearchBarDelegate를 구현하므로 검색이 시작했을 때 확인할 수 있습니다. 내 API 호출이 호출되는 반환하고 결과의 사전이 self.searchResults 속성에 저장할 때 내가 그렇게 블록을 설정 : 이제
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
// here we make the api call
[api getSomeInfo:searchBar.text complete:^(NSDictionary *json) {
self.searchResults = json;
[self.searchController.searchResultsTableView reloadData];
}];
}
, 내가 가지고있는 문제는 내 UITableViewDataSource 방법에 어디 사용자 정의 셀을 리턴하십시오. 내 세포의 인스턴스가 있지만 IBOutlets가 초기화되지는 결코, 그래서 내가 제대로 (등 텍스트, 이미지,) 그 내용 설정할 수 없습니다 :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == self.searchController.searchResultsTableView) {
cell = [tableView dequeueReusableCellWithIdentifier:@"SearchResultsCellIndentifier"];
if (cell == nil) {
cell = [[SearchResultsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.customLabel.text = [self.searchResults objectForKey:@"customText"]; // cell exists but cell.customLabel is nil!!
}
}
왜 콘텐츠 전무가를? 사용자 지정 셀 클래스에 콘텐츠를 설정해야하는 곳이 있습니까?
감사합니다.
인터페이스 빌더를 사용하여 사용자 정의 셀을 작성했음을 지적하고 싶습니다. 그것은 UIViewController의 프로토 타입 셀 아래에 있으며 식별자가 @ "SearchResultsCellIndentifier" –
여기에 대한 답변을 찾았습니다. http://bit.ly/y4XXen 재사용 가능한 셀을 dequeue하고 아무 것도 얻을 때 문제가 발생합니다. 그런 다음 새 셀을 만들려고했습니다. 사실 UIViewController의 tableView에서 재사용 가능한 셀을 dequeue해야했습니다. –