例外' NSRangeException',原因:' *** - [__ NSArrayM objectAtIndex:]:索引19超出界限[0 .. 18]'

时间:2014-03-07 20:31:06

标签: ios nsarray parse-platform searchbar nsexception

我已经找过其他类似的帖子了,但没有一个能解决我的问题。我收到这个错误:

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

我正在使用Searchbar和Display Controller从PARSE.COM数据库获取数据。

这是我的代码:

#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

这是堆栈:

*** 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

1 个答案:

答案 0 :(得分:7)

此异常是因为您更新findObjectsInBackgroundWithBlock中的数组,但tableView正在主线程callbackLoadObjectsFromParse上更新。

tableView 19行,但数组只有 18