搜索栏工作正常,但当我想设置 table 单元格标签时崩溃

Search bar works correctly but crashes when I want to set table cell label

我正在尝试使用我的 table 视图实现搜索栏。我的 table 视图和搜索栏都是使用故事板放置的。在我的 ViewDidLoad 中,我有:

self.searchDisplayController.delegate = self;
self.searchDisplayController.searchResultsDataSource = self;
self.searchDisplayController.searchResultsDelegate = self;

并实现了两种搜索方法:

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
    NSPredicate *bPredicate = [NSPredicate predicateWithFormat:@"nickname contains[cd] %@",searchText];
    NSArray* array = [[NSArray alloc]init];
    array = [self.fbfriendList filteredArrayUsingPredicate:bPredicate];
    self.searchResult = [NSMutableArray arrayWithArray:array];
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    [self filterContentForSearchText:searchString scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
    return YES;
}

在我的table查看方法中:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"addFriend";

    cell = [self.tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

if (cell == nil) {
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"FFAddFriendTableViewCell" owner:self options:nil];
    cell = [nib objectAtIndex:0];
}
if (tableView == self.searchDisplayController.searchResultsTableView)
{
    NSLog(@"!!! %@",self.searchResult);
    if (cell == nil) {
         [self.searchDisplayController.searchResultsTableView registerNib:[UINib nibWithNibName:@"FFAddFriendTableViewCell" bundle:nil] forCellReuseIdentifier:@"addFriend"];
    }
    else{
    NSLog(@"!!! %@",self.searchResult);
 // LINE XXXXXXX
        cell.nameLabel.text = [[self.searchResult objectAtIndex:indexPath.row] objectForKey:@"nickname"]; 
    }
}
else
{
}
cell.nameLabel.text = [[self.fbfriendList objectAtIndex:indexPath.row] objectForKey:@"nickname"];
return cell;
}

在搜索方面一切正常。我的 NSLogs 在我的数组中打印出正确的数据。但是,当我想根据 NSMutableArray 中的数据设置单元格的标签时,它崩溃并出现错误:

-[__NSCFString setText:]: unrecognized selector sent to instance 0x7ff38b5b70e0

我是否遗漏了在我的 tableView 中实施搜索栏的任何关键基础知识?

在“// LINE XXXXXX”下方的行中,

cell.nameLabel = ...

应该是,

cell.nameLabel.text = ...

现在将您的 table 查看 cellrowindex 方法更改为此..

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"addFriend";

    cell = [self.tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"FFAddFriendTableViewCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    if (tableView == self.searchDisplayController.searchResultsTableView)
    {
        cell.nameLabel.text = [[self.searchResult objectAtIndex:indexPath.row] objectForKey:@"nickname"];
    } else
    {
       cell.nameLabel.text = [[self.fbfriendList objectAtIndex:indexPath.row] objectForKey:@"nickname"];
    }

    return cell;
}