Exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 19 beyond bounds [0 .. 18]'

StackOverflow https://stackoverflow.com/questions/22260112

سؤال

I haver already looked for the other similar post, but none has solved my problem. I get this error:

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

I'm using a Searchbar and Display Controller getting data from a PARSE.COM database.

This is my code:

#import "busquedaViewController.h"

@interface busquedaViewController ()

@end

@implementation busquedaViewController

@synthesize totaldetalles;
@synthesize resultados;
@synthesize busquedaTableView;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;

}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

   // [self.activityIndicatorView startAnimating];
}

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
   // [self.resultados removeAllObjects];
    PFQuery *busqueda = [PFQuery queryWithClassName:@"icd_detalle"];
    [busqueda whereKey:@"DESC_ES" containsString:[searchText uppercaseString]];
    [busqueda findObjectsInBackgroundWithBlock:^(NSArray *results, NSError *error) {
        if(!error){
            resultados = [[NSMutableArray alloc] initWithArray:results];
            NSLog(@"ACA LA DATA YA FILTRADA ...%@ ... TADA", resultados);
        } else {
            NSLog(@"error en la busqueda");
        }
    }];
}

 -(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    [self filterContentForSearchText:searchString
                               scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
                                      objectAtIndex:[self.searchDisplayController.searchBar
                                                     selectedScopeButtonIndex]]];

    return YES;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

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

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.tableView){
        return 1;
    } else {
        return self.resultados.count;
   }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}
- (void)callbackLoadObjectsFromParse:(NSArray *)results error:(NSError *)error {
    if (!error) {
        [self.resultados removeAllObjects];

        [self.resultados addObjectsFromArray:results];
        [self.searchDisplayController.searchResultsTableView reloadData];
    } else {
        NSLog(@"error: %@ %@", error, [error userInfo]);
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *Identifier =@"bus1";

    busquedaCell *cell = (busquedaCell *)[self.tableView dequeueReusableCellWithIdentifier:Identifier];
  // busquedaCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier];

    if (cell == nil){
        cell= [[busquedaCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identifier];
    }

    if (tableView == self.tableView) {
        cell.busquedaDESC.text = @"Introdusca Busqueda";
    } else if (tableView == self.searchDisplayController.searchResultsTableView) {
        PFObject *tempDict = [self.resultados objectAtIndex:indexPath.row];
    cell.busquedaDESC.text = [tempDict objectForKey:@"DESC_ES"];
    cell.busquedaCode.text = [tempDict objectForKey:@"COD_4"];
   // [self.activityIndicatorView stopAnimating];

    }
    return cell;
}
@end

This is the stack:

*** First throw call stack:
(
    0   CoreFoundation                      0x00000001028ac795 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000010260f991 objc_exception_throw + 43
    2   CoreFoundation                      0x00000001028528e5 -[__NSArrayM objectAtIndex:] + 213
    3   icd2                                0x0000000100006956 -[busquedaViewController tableView:cellForRowAtIndexPath:] + 614
    4   UIKit                               0x0000000100df6b8a -[UITableView _createPreparedCellForGlobalRow:withIndexPath:] + 348
    5   UIKit                               0x0000000100dde836 -[UITableView _updateVisibleCellsNow:] + 2297
    6   UIKit                               0x0000000100def381 -[UITableView layoutSubviews] + 207
    7   UIKit                               0x0000000100d86b27 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 354
    8   QuartzCore                          0x000000010098ba22 -[CALayer layoutSublayers] + 151
    9   QuartzCore                          0x0000000100980589 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 363
    10  QuartzCore                          0x000000010098040a _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
    11  QuartzCore                          0x00000001008f5694 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 252
    12  QuartzCore                          0x00000001008f670c _ZN2CA11Transaction6commitEv + 394
    13  QuartzCore                          0x00000001009ae3bf _ZN2CA7Display11DisplayLink14dispatch_itemsEyyy + 485
    14  CoreFoundation                      0x000000010286ee24 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 20
    15  CoreFoundation                      0x000000010286e9a2 __CFRunLoopDoTimer + 962
    16  CoreFoundation                      0x000000010285796e __CFRunLoopRun + 1614
    17  CoreFoundation                      0x0000000102856f33 CFRunLoopRunSpecific + 467
    18  GraphicsServices                    0x00000001045833a0 GSEventRunModal + 161
    19  UIKit                               0x0000000100d2b043 UIApplicationMain + 1010
    20  icd2                                0x00000001000051a3 main + 115
    21  libdyld.dylib                       0x00000001032135fd start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
هل كانت مفيدة؟

المحلول

This exception is because you update the array in findObjectsInBackgroundWithBlock but tableView is updating on the main thread callbackLoadObjectsFromParse.

tableView has 19 rows but array only has 18.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top