저는 수십 개의 서로 다른 블로그를 읽고 몇 시간을 보냈습니다. 다른 사람들의 블로그는 여기에 있습니다.하지만 저는 막 다른 골목에 있습니다.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 :).
일부 코드를 보지 않으면 무엇이 잘못 될지 알기가 어렵습니다. – conmulligan