0

我试图显示包含三个单元格的分组表格视图,每个单元格都带有一个UISwitch。 我以为我有一切设置,但是当我从应用程序的主屏幕延伸到视图控制器,NSRangeException引发某处,我的应用程序崩溃。以下是最新崩溃的堆栈跟踪:当试图显示表视图控制器时出现NSRangeException

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 

这是我的表视图控制器的代码:

#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 

如果settingsEntries只包含一个项目,一切正常。一旦有两个或更多的项目,应用程序崩溃。当调用numberOfRowsInSection时,[settingsEntries count]正确返回3,应用程序在此之后崩溃,但在cellForRowAtIndexPath之前崩溃。该功能从未达到。

我在做什么错?

+0

数量固定的问题是否有任何的tableView:heightForRowAtIndexPath :?首先你的代码看起来很好。您也可以添加一个异常断点并查看异常实际抛出的位置。 –

+0

尝试注释掉'settingsEntries'并在部分中返回示例3行,看看会发生什么。 – juniperi

+0

@MANIAK_dobrii没有,我直接从Xcode的编辑器窗口粘贴了这段代码。我尝试添加一个异常断点,但它永远不会被触发,这非常奇怪。 –

回答

0

原来,将表格视图的内容类型设置为“静态单元格”会导致它无视在numberOf *函数中返回的任何值。显然我已经设置为显示一个静态单元格,这就是为什么当我尝试显示多个单元格时应用程序崩溃。 我通过设置内容类型回动态原型和原型细胞的0

DERP

相关问题