0

I've been stuck with this problem for hours and I can't see the light. Please give me a hand with this:

I have a tableview and a searchbar. The searchbar is situated in the navigationbar. When I do a fast scroll of the tableview, if I select the searchbar while the tableview is still decelerating, a exception raises:

Terminating app due to uncaught exception 'NSRangeException', reason: ' -[NSMutableArray objectAtIndex:]: index 31 beyond bounds for empty array'

How can I stop programmatically the deceleration of the tableview?

Thanks for your time!

Samui
  • 1,384
  • 3
  • 14
  • 28

2 Answers2

0

It's somewhat difficult to solve the error without having the code snippet.Then also what I think from the error is that after scrolling the tableview & the selecting the searchbar at that time you are making your Array null or either removing all objects from it.

Looking at the error it seems that when select the SearchBar Terminating app due to uncaught exception 'NSRangeException', reason: ' -[NSMutableArray objectAtIndex:]: index 31 beyond bounds for empty array'

  • (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar;

while on the other hand table once scrolled creating the new cells looking for the indexPath objects in the Array .But since all the objects has been removed from array it just crash you app & showing this error.

So what I suggest you take two different array the one that you have with full records & then other for search result & then reload the table using the array.

you will get rid of this error surely.Otherwise try to make sure you are not removing all the objects from array until the search is complete.

This is the logic you can use for searching. :)

  • (void)searchBar:(UISearchBar *)theSearchBar textDidChange:(NSString *)searchText {

    //Remove all objects first. copyListOfItems = [[NSMutableArray alloc] init]; [copyListOfItems removeAllObjects];

    if([searchText length] > 0) {

    searching = YES;
    tblView.scrollEnabled = YES;
    NSString *searchText = searchBar.text;
    NSLog(@"%@",searchText);
    NSMutableArray *searchArray = [[NSMutableArray alloc] init];
    
    for (NSMutableDictionary *dictionary in YourMainArray)
    {
        NSRange titleResultsRange = [[dictionary valueForKey:@"English"] rangeOfString:searchText options:NSCaseInsensitiveSearch];
    
        if (titleResultsRange.length > 0)
            [copyListOfItems addObject:dictionary];
    }
    
    [searchArray release];
    searchArray = nil;
    

    } else {

    searching = NO;
    tblView.scrollEnabled = YES;
    [searchBar resignFirstResponder];
    

    }

    [tblView reloadData]; }

Ajay Sharma
  • 4,509
  • 3
  • 32
  • 59
0

Finally I found the solution!

How can I programmatically force-stop scrolling in a UIScrollView?

Community
  • 1
  • 1
Samui
  • 1,384
  • 3
  • 14
  • 28