搜索栏导致应用程序在我的ios库中崩溃



我正在研究一个外部动态可可库(dylib),它在某些时候提供了一个带有表格视图和搜索栏的视图。我在创建时有这个

- (void)viewDidLoad {
    [super viewDidLoad];
    NSBundle *frameworkBundle = [NSBundle bundleForClass:[self class]];
    UINib *nib = [UINib nibWithNibName:CONTACT_CELL bundle:frameworkBundle];
    [[self tableView] registerNib:nib forCellReuseIdentifier:CONTACT_CELL];
    [self readContacts];
}

和我的表行代表

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ContactCell *cell = [tableView dequeueReusableCellWithIdentifier:CONTACT_CELL];
    if (cell == nil)
    {
        NSLog(@"Cell was empty!");
    }
    Contact *c = [contacts objectAtIndex:[indexPath row]];
    [[cell labelName] setText: [c getName]];
    [[cell labelPhone] setText: [c getPhone]];
    return cell;
} 

问题是,当我单击搜索栏时,应用程序会崩溃,因为:

* 断言失败 -[UISearchResultsTableView _configureCellForDisplay:forIndexPath:],/BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3512.30.14/UITableView.m:7962 2016-02-19 17:07:00.404 HostApp[2233:20181] * 由于以下原因终止应用程序 未捕获的异常"NSInternalInconsistencyException",原因: 'UITableView (; layer = ; contentOffset: {0, 0}; contentSize: {414, 528}>) 无法从其数据源 ()中获取单元格


如果我使用

[tableView dequeueReusableCellWithIdentifier:CONTACT_CELL forIndexPath: indexPath];

* 断言失败 in -[UISearchResultsTableView dequeueReusableCellWithIdentifier:forIndexPath:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3512.30.14/UITableView.m:6564 2016-02-19 17:37:08.254 HostApp[2691:28849] * 由于以下原因终止应用程序 未捕获的异常"NSInternalInconsistencyException",原因:"无法 使用标识符 ContactCell 取消对单元格的单元的排队 - 必须注册一个 nib 或 标识符的类或连接原型单元 故事板'

您无法在 cellForRowAtIndexPath 中返回nil

您可以使用:

[tableView dequeueReusableCellWithIdentifier:CONTACT_CELL forIndexPath: indexPath];

它总是返回一个单元格,或手动分配它:

 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ContactCell *cell = [tableView dequeueReusableCellWithIdentifier:CONTACT_CELL];
    if (cell == nil)
    {
        NSLog(@"Cell was empty!");
        cell = [UITableViewCell alloc] initWithStyle:<#(UITableViewCellStyle)#> reuseIdentifier:<#(nullable NSString *)#>];
    }
    Contact *c = [contacts objectAtIndex:[indexPath row]];
    [[cell labelName] setText: [c getName]];
    [[cell labelPhone] setText: [c getPhone]];
    return cell;
}

检查1.搜索您返回的内容的栏委托-(BOOL)searchBarShouldBeginEditor:(UISearchBar *)searchBar{}2.检查部分表中的行数视图删除

像这样尝试

ItemCell我的 nibName 不是标识符

- (void)viewDidLoad
    {
     [super viewDidLoad];
     UINib *nib = [UINib nibWithNibName:@"ItemCell" bundle:nil];
     [[self tableView] registerNib:nib forCellReuseIdentifier:@"ItemCell"];
    }
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
           // Create an instance of ItemCell
   PointsItemCell *cell =  [tableView dequeueReusableCellWithIdentifier:@"ItemCell"];
        return cell;
}

问题是我为"普通"表视图注册了笔尖,但没有为搜索显示控制器的表视图注册笔尖。

使用此视图DidLoad 将解决此问题。

- (void)viewDidLoad {
    [super viewDidLoad];
    NSBundle *frameworkBundle = [NSBundle bundleForClass:[self class]];
    UINib *nib = [UINib nibWithNibName:CONTACT_CELL bundle:frameworkBundle];
    [[self tableView] registerNib:nib forCellReuseIdentifier:CONTACT_CELL];
    [self.searchDisplayController.searchResultsTableView  registerNib:nib forCellReuseIdentifier:CONTACT_CELL];
    [self readContacts];
}

最新更新