【问题标题】:Searchbar cause app to crash in my ios library搜索栏导致应用程序在我的 ios 库中崩溃
【发布时间】:2016-02-19 16:31:16
【问题描述】:

我正在开发一个外部动态可可库 (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];
}

对于我的 tablerow 委托

- (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}>) 未能从其dataSource()'中获取单元格


如果我使用

[tableView dequeueReusableCellWithIdentifier:CONTACT_CELL forIndexPath: indexPath];

* -[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 或 标识符的类或连接原型单元 故事板'

【问题讨论】:

标签: ios objective-c iphone uitableview dylib


【解决方案1】:

您不能在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;
}

【讨论】:

  • 你 viewDidLoad 中UINib *nib = [UINib nibWithNibName:CONTACT_CELL bundle:frameworkBundle]; 的结果是什么?
  • 不为空。我必须以这种方式实现,因为自定义单元 xib 不会像我每天那样加载(因为我正在实现一个外部库)。
  • 你笔尖的唯一视图是UITableViewCell ?
  • 原谅?我有一个带有 tableview 和搜索栏的 xib,还有一个用于单元格的 xib。当我在搜索栏中输入内容时应用程序崩溃
【解决方案2】:

检查 1.搜索栏代表您返回的内容 -(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{} 2.查看section tableview deleage中的行数

【讨论】:

    【解决方案3】:

    这样试试

    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;
    }
    

    【讨论】:

    • 您的 viewDidLoad 对我不起作用,因为我在我的框架内而不是我的主应用程序中编码(捆绑:nil 不起作用)。
    【解决方案4】:

    问题是我为“普通”tableView 注册了 nib,但没有为 searchdisplaycontroller 的 tableView 注册笔尖。

    使用这个 viewDidLoad 将解决问题。

    - (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];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-01
      • 1970-01-01
      相关资源
      最近更新 更多