Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 搜索栏工作正常,但在设置表格单元格标签时崩溃_Ios_Objective C_Uisearchbar - Fatal编程技术网

Ios 搜索栏工作正常,但在设置表格单元格标签时崩溃

Ios 搜索栏工作正常,但在设置表格单元格标签时崩溃,ios,objective-c,uisearchbar,Ios,Objective C,Uisearchbar,我正在尝试用我的表视图实现一个搜索栏。我的表格视图和搜索栏都是使用故事板放置的。在我的ViewDidLoad中,我有: self.searchDisplayController.delegate = self; self.searchDisplayController.searchResultsDataSource = self; self.searchDisplayController.searchResultsDelegate = self; 并实现了两种搜索方法: - (void)fil

我正在尝试用我的表视图实现一个搜索栏。我的表格视图和搜索栏都是使用故事板放置的。在我的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;
}
在我的表视图方法中:

-(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中实现搜索栏的任何重要基础知识?

在“//XXXXXX行”下面的行中,您已经

cell.nameLabel = ...
应该是,

cell.nameLabel.text = ...
在“//行XXXXXX”下面的行中,您有

cell.nameLabel = ...
应该是,

cell.nameLabel.text = ...
在“//行XXXXXX”下面的行中,您有

cell.nameLabel = ...
应该是,

cell.nameLabel.text = ...
在“//行XXXXXX”下面的行中,您有

cell.nameLabel = ...
应该是,

cell.nameLabel.text = ...

现在将您的表视图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;
}

现在将您的表视图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;
}

现在将您的表视图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;
}

现在将您的表视图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;
}

新手犯了我的错误。我已将其更改为cell.namelab.text。它现在没有崩溃,但没有显示正确的单元格。如果有两个结果,表格将只显示主表格中的前两个单元格。“我错过了什么吗?”埃里克庄,我不确定我是否理解你的问题。如果只有2个结果,你应该在搜索结果表中只看到2个单元格。我已将其更改为cell.namelab.text。它现在没有崩溃,但没有显示正确的单元格。如果有两个结果,表格将只显示主表格中的前两个单元格。“我错过了什么吗?”埃里克庄,我不确定我是否理解你的问题。如果只有2个结果,你应该在搜索结果表中只看到2个单元格。我已将其更改为cell.namelab.text。它现在没有崩溃,但没有显示正确的单元格。如果有两个结果,表格将只显示主表格中的前两个单元格。“我错过了什么吗?”埃里克庄,我不确定我是否理解你的问题。如果只有2个结果,你应该在搜索结果表中只看到2个单元格。我已将其更改为cell.namelab.text。它现在没有崩溃,但没有显示正确的单元格。如果有两个结果,表格将只显示主表格中的前两个单元格。“我错过了什么吗?”埃里克庄,我不确定我是否理解你的问题。如果只有2个结果,您应该在搜索结果表中只看到2个单元格。