I'm trying to display a grouped table view containing three cells, each with a UISwitch. I thought I had everything set up, but when I segue to the view controller from my app's main screen, an NSRangeException is thrown somewhere and my app crashes. Here's the stacktrace of the latest crash:

2014-03-12 17:19:31.128 Sun Compass[2363:60b] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
*** First throw call stack:
(
    0   CoreFoundation                      0x01a2a1e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x015778e5 objc_exception_throw + 44
    2   CoreFoundation                      0x019de8b2 -[__NSArrayI objectAtIndex:] + 210
    3   UIKit                               0x0079227f -[UITableViewDataSource tableView:heightForRowAtIndexPath:] + 127
    4   UIKit                               0x0050bef1 -[UITableViewController tableView:heightForRowAtIndexPath:] + 76
    5   UIKit                               0x0049d6cc __66-[UISectionRowData refreshWithSection:tableView:tableViewRowData:]_block_invoke + 462
    6   UIKit                               0x0049cfd9 -[UISectionRowData refreshWithSection:tableView:tableViewRowData:] + 4187
    7   UIKit                               0x004a1e2f -[UITableViewRowData rectForFooterInSection:heightCanBeGuessed:] + 288
    8   UIKit                               0x004a1f40 -[UITableViewRowData heightForTable] + 68
    9   UIKit                               0x0030b701 -[UITableView _updateContentSize] + 400
    10  UIKit                               0x00327cdd -[UITableView setContentInset:] + 329
    11  UIKit                               0x00350e5a -[UIViewController _setNavigationControllerContentInsetAdjustment:] + 538
    12  UIKit                               0x0037c1ec -[UINavigationController _computeAndApplyScrollContentInsetDeltaForViewController:] + 399
    13  UIKit                               0x0037c35e -[UINavigationController _layoutViewController:] + 64
    14  UIKit                               0x0037b966 -[UINavigationController _layoutTopViewController] + 176
    15  UIKit                               0x00379b95 -[UINavigationController navigationTransitionView:didEndTransition:fromView:toView:] + 429
    16  UIKit                               0x0057d74e -[UINavigationTransitionView _notifyDelegateTransitionDidStopWithContext:] + 328
    17  UIKit                               0x0057da53 -[UINavigationTransitionView _cleanupTransition] + 703
    18  UIKit                               0x0057da92 -[UINavigationTransitionView _navigationTransitionDidStop] + 55
    19  UIKit                               0x002876dc -[UIViewAnimationState sendDelegateAnimationDidStop:finished:] + 267
    20  UIKit                               0x00285ec5 +[UIViewAnimationState popAnimationState] + 334
    21  UIKit                               0x0029b123 +[UIView(Animation) commitAnimations] + 36
    22  UIKit                               0x0057d551 -[UINavigationTransitionView transition:fromView:toView:] + 2795
    23  UIKit                               0x0057ca5e -[UINavigationTransitionView transition:toView:] + 55
    24  UIKit                               0x0037d577 -[UINavigationController _startTransition:fromViewController:toViewController:] + 3186
    25  UIKit                               0x0037d8cc -[UINavigationController _startDeferredTransitionIfNeeded:] + 645
    26  UIKit                               0x0037e4e9 -[UINavigationController __viewWillLayoutSubviews] + 57
    27  UIKit                               0x004bf0d1 -[UILayoutContainerView layoutSubviews] + 213
    28  UIKit                               0x002a6964 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 355
    29  libobjc.A.dylib                     0x0158982b -[NSObject performSelector:withObject:] + 70
    30  QuartzCore                          0x0189145a -[CALayer layoutSublayers] + 148
    31  QuartzCore                          0x01885244 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
    32  QuartzCore                          0x018850b0 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 26
    33  QuartzCore                          0x017eb7fa _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 294
    34  QuartzCore                          0x017ecb85 _ZN2CA11Transaction6commitEv + 393
    35  QuartzCore                          0x017ed258 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92
    36  CoreFoundation                      0x019f236e __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30
    37  CoreFoundation                      0x019f22bf __CFRunLoopDoObservers + 399
    38  CoreFoundation                      0x019d0254 __CFRunLoopRun + 1076
    39  CoreFoundation                      0x019cf9d3 CFRunLoopRunSpecific + 467
    40  CoreFoundation                      0x019cf7eb CFRunLoopRunInMode + 123
    41  GraphicsServices                    0x03a965ee GSEventRunModal + 192
    42  GraphicsServices                    0x03a9642b GSEventRun + 104
    43  UIKit                               0x00237f9b UIApplicationMain + 1225
    44  Sun Compass                         0x0000871d main + 141
    45  libdyld.dylib                       0x020e9701 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

This is my table view controller's code:

#import "SettingsTableViewController.h"

@interface SettingsTableViewController ()
{
    NSArray *settingsEntries;
}
@end

@implementation SettingsTableViewController

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

- (void)viewDidLoad
{
    [super viewDidLoad];

    settingsEntries = @[
                        @[@"Use True North", @1],
                        @[@"wat", @0],
                        @[@"˙ ͜ʟ˙", @1]
                    ];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark - Table view data source

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [settingsEntries count];
}

NSString *cellIdentifier= @"Cell";
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

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

//  NSString *cellText = settingsEntries[indexPath.row][0];
//  BOOL cellSwitch = (BOOL)settingsEntries[indexPath.row][1];
    NSString *cellText = @"Testing";
    BOOL cellSwitch = NO;

    cell.textLabel.text = cellText;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    UISwitch *switchView = [[UISwitch alloc] init];
    cell.accessoryView = switchView;
    [switchView setOn:cellSwitch animated:NO];
//  [switchView addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];

    return cell;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

If settingsEntries only contains one item, everything works. Once there's two or more items, the app crashes. When numberOfRowsInSection is called, [settingsEntries count] correctly returns 3, the app crashes somewhere after this, but before cellForRowAtIndexPath. That function is never reached.

What am I doing wrong?

有帮助吗?

解决方案

Turns out that setting your table view's content type to "Static Cells" causes it to disregard whatever value you return in the numberOf* functions. Apparently I had it set to display one static cell, that's why the app crashed whenever I tried to display more than one cell. I fixed the problem by setting the content type back to Dynamic Prototypes and the number of prototype cells to 0.

derp

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top