Search code examples
iosobjective-cuitableviewuisearchresultscontroller

App is crashing while entering text on search bar for tableview objective c


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
if (tableView == self.searchDisplayController.searchResultsTableView) {
   return searchResults.count; 
} else { 
    return self.countOfLocArr.count;
} 
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    AtmLocationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; 
    if(!cell) { 
        AtmLocationTableViewCell *cell = [[AtmLocationTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    } 
    NSMutableDictionary *locDictionary; 
    if(tableView == self.searchDisplayController.searchResultsTableView) {
     locDictionary = searchResults[indexPath.row];
    }else{ } 
    cell.placeName.text = locDictionary[@"name"]; 
    return cell;
    }

Getting error

* Assertion failure in -[UISearchResultsTableView _configureCellForDisplay:forIndexPath:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3512.60.7/UITableView.m:7971 2016-12-01 18:21:04.096 Mobee[6459:2145589] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView (; layer = ; contentOffset: {0, 0}; contentSize: {320, 396}>) failed to obtain a cell from its dataSource ()' *** First throw call stack: (0x20fb9b8b 0x20776dff 0x20fb9a61 0x217a072b 0x256d64a3 0x258d3cb9 0x258d3d91 0x258c326d 0x256673bd 0x2579129f 0x256ec46d 0x257923e3 0x257920bb 0x25791f2d 0x255c2521 0x255c24b1 0x255aa3eb 0x25791c75 0x255ccd9f 0x25760b5b 0x2575feed 0x258e967f 0x25ea5f73 0x2575fc53 0x2598e135 0x2598dd5f 0x255b5355 0x217e6867 0x20f7ba67 0x20f7b657 0x20f799bf 0x20ec8289 0x20ec807d 0x224e4af9 0x255f32c5 0xde303 0x20b74873) libc++abi.dylib: terminating with uncaught exception of type NSException


Solution

  • Check this :

    1) You are returning a number larger than your Array Count from tableView:numberOfRowsInSection:. Don't.

    2) One or more of your cell# outlets is not hooked up in your nib, or is not hooked up to a UITableViewCell (or subclass). Hook them up properly.

    refer for more :https://stackoverflow.com/a/14192093/3901620