未被捕获的异常' NSRangeException',原因:' *** - [__ NSArrayI objectAtIndex

时间:2014-11-26 16:18:51

标签: ios iphone uitableview

我是目标C的新手,我想知道是否有人可以帮我弄清楚为什么我的应用程序不断崩溃。我相信它与我的阵列有关,但我不知道如何解决它..有人可以帮忙吗??? ..

以下是崩溃时的日志输出:

2014-11-26 10:37:42.172 SamplePhotoReDo[89697:109203550] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 2 beyond bounds [0 .. 0]'
    *** First throw call stack:
     (
0   CoreFoundation                      0x000000010c75af35 __exceptionPreprocess + 165
1   libobjc.A.dylib                     0x000000010c09cbb7 objc_exception_throw + 45
2   CoreFoundation                      0x000000010c65301e -[__NSArrayI objectAtIndex:] + 190
3   UIKit                               0x000000010d67474a -[UITableViewDataSource tableView:heightForHeaderInSection:] + 39
4   UIKit                               0x000000010d221f4e -[UITableView _delegateWantsHeaderForSection:] + 261
5   UIKit                               0x000000010d3a6983 -[UISectionRowData refreshWithSection:tableView:tableViewRowData:] + 162
6   UIKit                               0x000000010d3ace45 -[UITableViewRowData rectForFooterInSection:heightCanBeGuessed:] + 320
7   UIKit                               0x000000010d3acf3a -[UITableViewRowData heightForTable] + 56
8   UIKit                               0x000000010d1ffaf0 -[UITableView _updateContentSize] + 381
9   UIKit                               0x000000010d21cecd -[UITableView didMoveToWindow] + 65
10  UIKit                               0x000000010d1a39a0 -[UIView(Internal) _didMoveFromWindow:toWindow:] + 1482
11  UIKit                               0x000000010d1b4333 -[UIScrollView _didMoveFromWindow:toWindow:] + 55
12  UIKit                               0x000000010d1a368e -[UIView(Internal) _didMoveFromWindow:toWindow:] + 696
13  UIKit                               0x000000010d19c112 __45-[UIView(Hierarchy) _postMovedFromSuperview:]_block_invoke + 125
14  UIKit                               0x000000010d19c086 -[UIView(Hierarchy) _postMovedFromSuperview:] + 437
15  UIKit                               0x000000010d1a5f4b -[UIView(Internal) _addSubview:positioned:relativeTo:] + 1604
16  UIKit                               0x000000010d486534 -[UINavigationTransitionView transition:fromView:toView:] + 479
17  UIKit                               0x000000010d2841ef -[UINavigationController _startTransition:fromViewController:toViewController:] + 2967
18  UIKit                               0x000000010d284487 -[UINavigationController _startDeferredTransitionIfNeeded:] + 523
19  UIKit                               0x000000010d284f47 -[UINavigationController __viewWillLayoutSubviews] + 43
20  UIKit                               0x000000010d3ca509 -[UILayoutContainerView layoutSubviews] + 202
21  UIKit                               0x000000010d1a8973 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 521
22  QuartzCore                          0x000000011052bde8 -[CALayer layoutSublayers] + 150
23  QuartzCore                          0x0000000110520a0e _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
24  QuartzCore                          0x000000011052087e _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
25  QuartzCore                          0x000000011048e63e _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 242
26  QuartzCore                          0x000000011048f74a _ZN2CA11Transaction6commitEv + 390
27  UIKit                               0x000000010d12d54d -[UIApplication _reportMainSceneUpdateFinished:] + 44
28  UIKit                               0x000000010d12e238 -[UIApplication _runWithMainScene:transitionContext:completion:] + 2642
29  UIKit                               0x000000010d12cbf2 -[UIApplication workspaceDidEndTransaction:] + 179
30  FrontBoardServices                  0x0000000112f002a3 __31-[FBSSerialQueue performAsync:]_block_invoke + 16
31  CoreFoundation                      0x000000010c69053c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
32  CoreFoundation                      0x000000010c686285 __CFRunLoopDoBlocks + 341
33  CoreFoundation                      0x000000010c686045 __CFRunLoopRun + 2389
34  CoreFoundation                      0x000000010c685486 CFRunLoopRunSpecific + 470
35  UIKit                               0x000000010d12c669 -[UIApplication _run] + 413
36  UIKit                               0x000000010d12f420 UIApplicationMain + 1282
37  SamplePhotoReDo                     0x000000010bb60453 main + 115
38  libdyld.dylib                       0x000000010f2d2145 start + 1
 )
  libc++abi.dylib: terminating with uncaught exception of type NSException
   (lldb)'

以下是可能导致崩溃的代码:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1 + self.collectionsFetchResults.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger numberOfRows = 0;
    if (section == 0) {
        numberOfRows = 1; // "All Photos" section
    } else {
        PHFetchResult *fetchResult = self.collectionsFetchResults[section - 1];
        numberOfRows = fetchResult.count;
    }
    return numberOfRows;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    NSString *localizedTitle = nil;

    if (indexPath.section == 0) {
        cell = [tableView dequeueReusableCellWithIdentifier:AllPhotosReuseIdentifier forIndexPath:indexPath];
        localizedTitle = NSLocalizedString(@"All Photos", @"");
     } else {
        cell = [tableView dequeueReusableCellWithIdentifier:CollectionCellReuseIdentifier forIndexPath:indexPath];
        PHFetchResult *fetchResult = self.collectionsFetchResults[indexPath.section - 1];
        PHCollection *collection = fetchResult[indexPath.row];
        localizedTitle = collection.localizedTitle;
    }
    cell.textLabel.text = localizedTitle;

    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSString *title = nil;
    if (section > 0) {
        title = self.collectionsLocalizedTitles[section - 1];
    }
    return title;
}

#pragma mark - PHPhotoLibraryChangeObserver

- (void)photoLibraryDidChange:(PHChange *)changeInstance
{
    // Call might come on any background queue. Re-dispatch to the main queue to handle it.
    dispatch_async(dispatch_get_main_queue(), ^{
        NSMutableArray *updatedCollectionsFetchResults = nil;
        for (PHFetchResult *collectionsFetchResult in self.collectionsFetchResults) {
            PHFetchResultChangeDetails *changeDetails = [changeInstance changeDetailsForFetchResult:collectionsFetchResult];
            if (changeDetails) {
                if (!updatedCollectionsFetchResults) {
                    updatedCollectionsFetchResults = [self.collectionsFetchResults mutableCopy];
                }
            [updatedCollectionsFetchResults replaceObjectAtIndex:[self.collectionsFetchResults indexOfObject:collectionsFetchResult] withObject:[changeDetails fetchResultAfterChanges]];
        }
    }
    if (updatedCollectionsFetchResults) {
        self.collectionsFetchResults = updatedCollectionsFetchResults;
        [self.tableView reloadData];
    }
    });
}

1 个答案:

答案 0 :(得分:1)

 reason: '*** -[__NSArrayI objectAtIndex:]: index 2 beyond bounds [0 .. 0]'

很明显,你的阵列没有你想象的那么多元素。在填充数组的位置放置一个断点,并按预期双重检查。