Question

I'm having some trouble with the sidebar on this app (that uses Storyboard) I'm working on. The sidebar is a UITableViewController and I want a search bar on the top, so I put the Search Bar and Search Display Controller object into Storyboard. I have the sidebar's contents in 5 static cells and the search bar makes a search to a remote database to retrieve the results.

My issue is that if my search results contain more than 5 elements, I get the following error:

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 5 beyond bounds [0 .. 4]'

I'm not exactly sure what's happening behind the scenes, but I'm fairly certain that despite having the following code, the number of rows set for the table view section in Storyboard (5) is overriding everything.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        return [[self filteredCappegoryArray] count];
    } else {
        return [super tableView:tableView numberOfRowsInSection:0];
    }
}

I would switch the sidebar to use dynamic cells, but one of my cells contains a container view, and XCode doesn't allow me to have a container view in a prototype cell. I was wondering if there are any options I have to work around this issue.

Was it helpful?

Solution

I managed to solve the error with the help of a colleague. After paying more attention to the call stack, we realized that the app was crashing in a datasource method (index 3).

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 5 beyond bounds [0 .. 4]'
*** First throw call stack:
(
0   CoreFoundation                      0x000000010188c795 __exceptionPreprocess + 165
1   libobjc.A.dylib                     0x00000001015ef991 objc_exception_throw + 43
2   CoreFoundation                      0x000000010184502f -[__NSArrayI objectAtIndex:] + 175
3   UIKit                               0x00000001006d97df -[UITableViewDataSource tableView:indentationLevelForRowAtIndexPath:] + 115
4   UIKit                               0x0000000100318a08 __53-[UITableView _configureCellForDisplay:forIndexPath:]_block_invoke + 1574
5   UIKit                               0x00000001002a60ac +[UIView(Animation) performWithoutAnimation:] + 70
[...]

So, I added this and the results display perfectly fine now.

- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 0;
}

This actually solved two issues, as I noticed my search results had varying indentations before adding that method as well.

OTHER TIPS

You could override:

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

And return a custom UITableViewCell in some cases and call the super in others, just like you have overriden numberOfRowsInSection

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top