我正在尝试绑定"搜索栏和搜索显示控制器"在我的UITableView中。过滤的数组具有正确的数据,但它不会在tableview中显示。当我在搜索栏中输入内容时,它会显示空白表格视图。
在函数" cellForRowAtIndexPath"当它绑定正确的数据但仍然没有显示。在同一个功能中,我也改变了代码
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@" PersonCell" forIndexPath:indexPath];
到
UITableViewCell * cell = [self.personTableView dequeueReusableCellWithIdentifier:@" PersonCell" forIndexPath:indexPath];
然后抛出以下异常: 由于未捕获的异常终止应用程序' NSInvalidArgumentException',原因:' - [人员长度]:无法识别的选择器发送到实例0x1780ca170'
代码:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
return [self.filteredPersonArray count];
} else {
return [self.personArray count];
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PersonCell" forIndexPath:indexPath];
Person *person;
if (tableView == self.searchDisplayController.searchResultsTableView) {
person = (Person *)[self.filteredPersonArray objectAtIndex:indexPath.row];
} else {
person = (Person *)[self.personArray objectAtIndex:indexPath.row];
}
UILabel *nameLabel = (UILabel *)[cell viewWithTag:1];
nameLabel.text = [NSString stringWithFormat:@"%@ %@", person.firstName, person.lastName];
// so on;
return cell;
}
-(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.filteredPersonArray removeAllObjects];
// Filter the array using NSPredicate
NSPredicate *lastNamepredicate = [NSPredicate predicateWithFormat:@"SELF.lastName contains[c] %@",searchText];
//NSPredicate *firstNamePredicate = [NSPredicate predicateWithFormat:@"SELF.firstName contains[c] %@",scope];
//tempArray = [tempArray filteredArrayUsingPredicate:lastNamepredicate];
self.filteredPersonArray = [NSMutableArray arrayWithArray:[self.personArray
filteredArrayUsingPredicate:lastNamepredicate]];
}
-(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;
}
答案 0 :(得分:0)
解决这个问题:
在as的情况下创建单元格时,我引用了不同的tableview对象
1.搜索后加载数据
2.在tableview中加载数据
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
Person *person;
if (tableView == self.searchDisplayController.searchResultsTableView)
{
cell = [self.personTableView dequeueReusableCellWithIdentifier:@"PersonCell"];
person = (Person *)[self.filteredPersonArray objectAtIndex:indexPath.row];
} else {
cell = [tableView dequeueReusableCellWithIdentifier:@"PersonCell"];
person = (Person *)[self.personArray objectAtIndex:indexPath.row];
}
UILabel *nameLabel = (UILabel *)[cell viewWithTag:1];
nameLabel.text = [NSString stringWithFormat:@"%@ %@", person.firstName, person.lastName];
// so on;
return cell;
}