简体   繁体   中英

Searchbar cause app to crash in my ios library

I'm working on an external dynamic cocoa library (dylib) that at some point provides a view with a tableview and a search bar. I have this on the on create

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

and for my tablerow delegate

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

problem is that when i click on the searchbar the app chrashes because:

* Assertion failure in -[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] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView (; layer = ; contentOffset: {0, 0}; contentSize: {414, 528}>) failed to obtain a cell from its dataSource ()'


if I use

[tableView dequeueReusableCellWithIdentifier:CONTACT_CELL forIndexPath: indexPath];

* Assertion failure 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] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier ContactCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'

You can't return nil in cellForRowAtIndexPath .

You can either use :

[tableView dequeueReusableCellWithIdentifier:CONTACT_CELL forIndexPath: indexPath];

Which always returns a cell, or manually alloc it :

 -(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.Search Bar委托以获取返回的内容-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {} 2.检查表tablele中的行数

Try like this

ItemCell is my nibName not an identifier

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

The problem was I registered the nib for the "normal" tableView, but not for the searchdisplaycontroller's tableView.

Using this viewDidLoad will fix the problem.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM