0

I have tableview with searchDisplayController. When I search for some text I successfully display the results on screen. My problem arises when I want to add a cell to existing results. Adding a cell must be done when searchDisplayController is active.

That is:

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        NSMutableDictionary * dict ;
        BOOL isSearchView = tableView == self.searchDisplayController.searchResultsTableView;
        if (isSearchView) {
            dict = [searchTasks objectAtIndex:indexPath.row];
            [self.searchDisplayController.searchResultsTableView deselectRowAtIndexPath:indexPath
                                                                               animated:YES];
        }
        else{
            dict = [self.tasks objectAtIndex:indexPath.row];
            [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
        }
            Task * task = [self getsomeTask];
            NSIndexPath *  ip = [NSIndexPath indexPathForRow:indexPath.row+1  
inSection:indexPath.section];
            if (isSearchView) {
                [searchTasks insertObject:task atIndex:ip.row];
                [self.searchDisplayController.searchResultsTableView  
                                        insertRowsAtIndexPaths:@[ip]
                                        withRowAnimation:UITableViewRowAnimationRight];

    }

after this is executed, and after the last line insertRowsAtIndexPaths:@[ip],

the code executes:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.searchDisplayController.isActive) {
        return [searchTasks count];
    } else {
        return [tasks count];
    }
}

Which is fine and here it selects the first option which is true according to program logic. But the it crashed and after this execution it never calls

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

delegate function of UITableview.

And gives me error:

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

I cannot understand the reason why it does not call the cellForRowAtIndexPath function after rowcount function.

Any suggestions?

Jeyhun Karimov
  • 1,295
  • 19
  • 27
  • Seems like `searchTasks` is an empty array and it's being accessed somewhere in your code before there is anything inside it. What is the exact line that the code is crashing? Can you include the code inside `tableView: cellForRowAtIndexPath:`? – Austen Chongpison Sep 07 '14 at 21:54
  • I put debugger on cellForRowAtIndexPath it does not gets called after numberOfRowsInSection function. And numberOfRowsInSection function returns 2 in this case, that is the array is not empty. I assume that once the searchdisplaycontroller tableview array is set on -(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString - function, it cannot be added new row until the function is called again. But why? – Jeyhun Karimov Sep 07 '14 at 22:10
  • Is there anything happening in a block in your table view code? You say that cellForRowAtIndexPath is not called. But it's still not clear exactly where your code is crashing. What line exactly is your code crashing on? From the NSRangeException that you reported the [searchTasks count]; line would not give you that error. – Austen Chongpison Sep 07 '14 at 22:36
  • Code is executing in this order : didSelectRowAtIndexPath => numberOfRowsInSection and crash. Again I assume that iOS do not permit to add or delete rows of displayviewcontroller. – Jeyhun Karimov Sep 08 '14 at 10:03

0 Answers0