Swemoph Swemoph - 3 months ago 54
iOS Question

UITableView crash despite cellForRowAtIndexPath returning valid cell

I'm having some issues with a crash occurring when I change view controllers - Basically I have a VC that takes data from the user for a search - it then brings up a new VC with search results, displayed in a UITableView (not using a

UITableViewController
, just a TableView inside a regular
UIViewController
)

Crash Log:



2016-08-29 20:48:03.950 MyApp[2596:60877] *** Assertion failure in -[SearchResultsTable _configureCellForDisplay:forIndexPath:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3512.60.7/UITableView.m:7971

2016-08-29 20:48:03.961 MyApp[2596:60877] Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView (; layer = ; contentOffset: {0, 0}; contentSize: {375, 1128}>) failed to obtain a cell from its dataSource (; layer = ; contentOffset: {0, 0}; contentSize: {0, 0}>)'
*** First throw call stack:



I've read that this issue is caused by not returning a cell in your
cellForRowAtIndexPath
function, however i'm positive that my implementation will return a cell successfully. (I tested the code of the function somewhere else, and
cell
does in fact have a value, and is not nil.)

Code:

@property searchResultCell * cell;


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

_cell = [self dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

if (_cell == nil)
{
_cell = [[searchResultCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}

[[_cell txtDate] setText:@"test"];
return _cell;
}


Having placed some breakpoints down, it appears that this section of code is never even executed, despite the
UITableView
class, delegate and dataSource being set correctly.

viewDidLoad for the Search Results VC (
[self SpecifiedSerial]
is set previously before the segue happens. It is an
NSString*
):

- (void)viewDidLoad
{
[super viewDidLoad];

_SearchResultsTableDelegate = [[searchResultsTable alloc] init:[self specifiedSerial]];

[[self SearchResultsTable] setDelegate:_SearchResultsTableDelegate];
[[self SearchResultsTable] setDataSource:_SearchResultsTableDelegate];
}


Declarations:

@property (weak, nonatomic) IBOutlet UITableView *SearchResultsTable;
@property searchResultsTable * SearchResultsTableDelegate;


If anyone could point me in the right direction here, it'd be much appreciated!

Answer

Use this code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    _cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

Instead of:

-(UITableViewCell*)cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     _cell = [self dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

Edit:

Your numberOfRowsInSection and numberOfSectionsInTableView seems to be incorrect.

Try this code:

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [[self searchedTrainSightings] count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    _cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell" forIndexPath:indexPath];

    if (_cell == nil)
    {
        _cell = [[MyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }

    [[_cell textLabel] setText:@"test"];
    return _cell;
}