0

我想要一個UISearchDisplayController和的UISearchBar添加到我的UIViewController用的UITableViewController用InterfaceBuilder的增加,這是我的代碼:UISearchDisplayController與自定義的UITableViewCell在第二搜索失去的UITableViewCell連接

@interface MyClass() <UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate, UISearchDisplayDelegate> 

{ 
UISearchBar *searchBar; 
UISearchDisplayController *searchDisplayController; 
} 

@property (weak, nonatomic) IBOutlet UITableView *tableView; 

@end 

@implementation MyClass 

- (void)viewDidLoad 
{ 
UINib *myCellNib = [UINib nibWithNibName:@"MyCell_iPhone" bundle:[NSBundle mainBundle]]; 
[self.tableView registerNib:myCellNib forCellReuseIdentifier:CellIdentifier]; 
[self.tableView setDataSource:self]; 
[self.tableView setDelegate:self]; 
[self.tableView setRowHeight:84]; 
[self.tableView setSectionHeaderHeight:45]; 

searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, -44 , 320, 44)]; 
searchBar.delegate = self; 
[searchBar setBackgroundImage:[UIImage imageNamed:@"NavBar.png"]]; 

[self.view addSubview:searchBar]; 

searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self]; 
searchDisplayController.delegate = self; 
searchDisplayController.searchResultsDataSource = self; 
searchDisplayController.searchResultsDelegate = self; 

[searchDisplayController.searchResultsTableView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"pattern_bg"]]]; 
[searchDisplayController.searchResultsTableView setSeparatorColor:[UIColor clearColor]]; 
[searchDisplayController.searchResultsTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone]; 
[searchDisplayController.searchResultsTableView registerNib:myCellNib forCellReuseIdentifier:CellIdentifier]; 
[searchDisplayController.searchResultsTableView setRowHeight:84]; 
[searchDisplayController.searchResultsTableView setSectionHeaderHeight:0]; 
} 

#pragma mark Content Filtering 
-(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope { 
// Update the filtered array based on the search text and scope. 
// Remove all objects from the filtered search array 
[self.filteredInfo removeAllObjects]; 
[self doLocalSearch:searchText]; 

} 


#pragma mark - UISearchDisplayController Delegate Methods 
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString { 
// Tells the table data source to reload when text changes 
[self filterContentForSearchText:searchString scope: 
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]]; 
// Return YES to cause the search result table view to be reloaded. 
return YES; 
} 

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption { 
// Tells the table data source to reload when scope bar selection changes 
[self filterContentForSearchText:self.searchDisplayController.searchBar.text scope: 
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]]; 
// Return YES to cause the search result table view to be reloaded. 
return YES; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
MyCell_iPhone *cell = (MyCell_iPhone *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if (tableView == searchDisplayController.searchResultsTableView) 
{ 
.....code to set information in cell 
} else { 
.....code to set information in cell 
} 

return cell; 
} 

我主要的UITableView完美的作品,而不是searchDisplayController.searchResultsTableView有這個問題,我第一次使搜索一切正常,我可以看到MyCustom單元格,我可以看到我在uitableview插入的背景,所以所有的作品,然後我按下取消按鈕關閉搜索我重新打開搜索視圖,我做了另一個搜索,我收到此錯誤:

*** Assertion failure in -[UISearchResultsTableView _configureCellForDisplay:forIndexPath:] 

我有插入大量的斷點,看看問題出在哪裏,我發現這裏:

MyCell_iPhone *cell = (MyCell_iPhone *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

第二搜索的價值爲零,表視圖失去它的背景,返回古典的白色背景,並崩潰,有什麼問題?我重複我的主要的UITableView的作品完美...

回答

2

我必須插入UISearchDisplayController表格視圖此委託方法的自定義設置:

- (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView 
{ 
[searchDisplayController.searchResultsTableView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"pattern_bg"]]]; 
[searchDisplayController.searchResultsTableView setSeparatorColor:[UIColor clearColor]]; 
[searchDisplayController.searchResultsTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone]; 
UINib *myCellNib = [UINib nibWithNibName:@"MyCell_iPhone" bundle:[NSBundle mainBundle]]; 
[searchDisplayController.searchResultsTableView registerNib:myCellNib forCellReuseIdentifier:CellIdentifier]; 
[searchDisplayController.searchResultsTableView setRowHeight:84]; 
[searchDisplayController.searchResultsTableView setSectionHeaderHeight:0]; 
} 
+0

相反searchDisplayController的,你應該使用所謂的控制器的第一個方法的參數。 – LightMan