Ken Toh Ken Toh - 7 months ago 57
Objective-C Question

Handling a tableview with static cells and another searchResultsTableView

Originally, I have a main table view controller with a tableview of static cells laid out using Storyboards. Subsequently, I added a SearchDisplayController to this UITableViewController. In my tableview data source delegate methods such as numberOfSectionsInTableView: and numberOfRowsInSection, I am differentiating between my own tableview (with the static cells) and the searchResultsTableView of the search display controller by checking the following:

if (tableView == self.searchDisplayController.searchResultsTableView)
{
// logic for searchResultsTableView;
}
else
{
// logic for my main tableView
}


As far as I know, this seems to be the right approach. But when I tried the following for the cellForRowAtIndexPath method, I am getting a crash with the message Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == self.searchDisplayController.searchResultsTableView)
{
// Just want to use a default cell. There seems to be no good way of specifying a prototype cell for this in the storyboard.
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if ( cell == nil ) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
return cell;
}
else
{
// how to handle this case?
return nil;
}
}


Previously, without the search display controller, I didn't have to implement this method as my cells are static. I guess my question is, how should I handle a hybrid case (in cellForRowAtIndexPath as well as specifying both kinds of cells in the same storyboard), where I have a search display controller tableview with dynamic cells and a tableview with static cells? I would imagine such a scenario to be not uncommon.

Thanks in advance!

EDIT:

While calling the super method in the else clause as suggested by the first commenter seemed to fix the crash, I am encountering yet another issue which I feel is somehow due to inherent issues of the tableViewController being a delegate of both a static tableview and a non-static one (the search display results table view).

The new issue: My static tableview has 2 static cells. When I populate my search results tableview with more than 2 rows, I get a NSRangeException', reason: -[__NSArrayI objectAtIndex:]: index 2 beyond bounds [0 .. 1].

It seems like the searchResultsTableView derived the number of rows from my main static tableview somehow (results were consistent when I added a 3rd static cell) even though the delegate method: numberOfRowsInSection was being fired for the case of searchResultsTableView, and returning the right number.

Any workarounds on making static tableviews play nice with search display tableviews? I am thinking of converting my main static tableview to one with dynamic cells.. Any other suggestions are welcome, thanks!

Answer Source

My workaround in the end was to convert the static tableview into one with dynamic cells. It isn't pretty but it works.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download