【问题标题】:UISearchDisplayController with custom UITableViewCell at the second search lose UITableViewCell connection在第二次搜索时带有自定义 UITableViewCell 的 UISearchDisplayController 失去 UITableViewCell 连接
【发布时间】:2013-08-22 10:03:26
【问题描述】:

我正在尝试将 UISearchDisplayController 和 UISearchBar 添加到我的 UIViewController 与 UITableViewController 添加与 InterfaceBuilder,这是我的代码:

@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 Cell,我可以看到我在 uitableview 中插入的背景,所以一切正常,然后我按下取消按钮关闭搜索我重新打开搜索视图,我再次搜索并收到此错误:

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

我已经插入了很多断点来查看问题出在哪里,我发现这里:

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

第二次搜索值为nil,table view失去了背景,返回经典的白色背景,然后crash,是什么问题?我重复我的主要 uitableview 工作完美......

【问题讨论】:

    标签: ios objective-c uitableview uisearchbar uisearchdisplaycontroller


    【解决方案1】:

    我必须在这个委托方法中插入 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];
    }
    

    【讨论】:

    • 你应该使用第一个方法的参数,而不是 searchDisplayController。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-08
    • 1970-01-01
    相关资源
    最近更新 更多