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之前崩潰。該功能從未達到。
我在做什麼錯?
數量固定的問題是否有任何的tableView:heightForRowAtIndexPath :?首先你的代碼看起來很好。您也可以添加一個異常斷點並查看異常實際拋出的位置。 –
嘗試註釋掉'settingsEntries'並在部分中返回示例3行,看看會發生什麼。 – juniperi
@MANIAK_dobrii沒有,我直接從Xcode的編輯器窗口粘貼了這段代碼。我嘗試添加一個異常斷點,但它永遠不會被觸發,這非常奇怪。 –