【问题标题】:xcode search bar crash try to enter somethingxcode搜索栏崩溃尝试输入一些东西
【发布时间】:2014-07-14 10:26:52
【问题描述】:

#pragma Search Methods

-(void)filterContentForSearchText:(NSString *)searchText scope:(NSString *)scope
{
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@",searchText];
    _AramaSonuclari = [_TarifAdi filteredArrayUsingPredicate:predicate];
    
}

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    [self filterContentForSearchText:searchString scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
    return YES;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    // Return the number of rows in the section.
    if (tableView == self.searchDisplayController.searchResultsTableView)
    {
        return [_AramaSonuclari count];
    }
    else
    {
        return _TarifAdi.count;
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TableViewCell" forIndexPath:indexPath];
    
    cell.tag = indexPath.row;
    //cell.imageView.image = nil;
    
    if (tableView == self.searchDisplayController.searchResultsTableView)
    {
        cell.TitleLabel.text = _AramaSonuclari[indexPath.row];
    }
    else
    {
        cell.TitleLabel.text = _TarifAdi[indexPath.row];
    }

    return cell;
 }

我们的问题,当我们尝试在搜索栏中输入任何字符时,应用程序崩溃,我们确实调试了我们的代码工作而没有错误。我们认为,我们的问题在于故事板连接;我还添加了一张图片。当我们删除 searchBar 引用的 outlet 时,我们可以输入一些东西,但当然代码在没有连接的情况下无法工作。

错误日志:

2014-07-14 13:29:08.577 SevgiLezzeti[3839:60b] *** 断言失败 -[UISearchResultsTableView dequeueReusableCellWithIdentifier:forIndexPath:], /SourceCache/UIKit_Sim/UIKit-2935.137/UITableView.m:5439

2014-07-14 13:29:08.582 SevgiLezzeti[3839:60b] *** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无法使具有标识符 TableViewCell 的单元格出列 - 必须注册一个 nib 或一个标识符的类或连接故事板中的原型单元'

【问题讨论】:

  • 什么是崩溃日志,你是如何确定你的代码是正确的
  • 您在 IB 中是否将单元格 id 定义为“TableViewCell”?
  • 是的,当我尝试 searchBar 时,我定义了我的代码工作没有搜索仍然没有问题,但是当我尝试输入内容时立即崩溃
  • 在将单元格出列时尝试使用 self.tableView 而不是 tableView,因为 tableView 可能不是已定义单元格 ID 的实例
  • 这与Xcode无关。

标签: ios objective-c


【解决方案1】:

您还必须将单元格注册到 searchdisplaycontroller。

Important: You must register a class or nib file using the registerNib:forCellReuseIdentifier: or registerClass:forCellReuseIdentifier: method before calling this method.

在视图中确实加载了

如果您在代码中创建单元格

 [self.searchDisplayController.searchResultsTableView registerClass:[TableViewCell class] 
    forCellReuseIdentifier:@"IdentifierForCell"];

如果你在 nib 中创建单元格

  [self.searchDisplayController.searchResultsTableView registerNib:[UINib nibWithNibName:@"CellNibName" bundle:nil]
                                        forCellWithReuseIdentifier:@"IdentifierForCell"];

【讨论】:

  • 现在我们看不到结果,如何解决这个问题?
  • 检查 NSPredicate。是否给出数据
【解决方案2】:

我就这样解决了:

在这一行:

TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TableViewCell" forIndexPath:indexPath];

你需要删除“forIndexPath:indexPath” 所以:

 TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TableViewCell"];

【讨论】:

  • 这解决了操作中的问题,但后来我得到“致命错误:在展开可选值时意外发现 nil”
  • 您正在快速使用,您应该检查其中一个字段是否不为零。 stackoverflow.com/questions/24643522/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-09
  • 2022-01-14
  • 2021-02-12
相关资源
最近更新 更多