我使用以下代碼在我的故事板中創建了一個表視圖。使用NSMutableArray的表的奇怪行爲
#import "FastestTapViewController.h"
@interface FastestTapViewController()
@end
@implementation FastestTapViewController
{
NSMutableArray *recipes;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Initialize table data
recipes = [NSMutableArray arrayWithObjects:@"Egg Benedict", @"Mushroom Risotto", @"Full Breakfast", @"Hamburger", @"Ham and Egg Sandwich", nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [recipes count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [recipes objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:@"creme_brelee.jpg"];
return cell;
}
@end
但同時向上滾動顯示如下EXEC_BAD_ACCESS錯誤
如果我使用添加對象見下文然後正常工作
- (void)viewDidLoad
{
[super viewDidLoad];
// Initialize table data
NSMutableArray *recipes = [[NSMutableArray alloc] init];
[recipes addObject:@"Egg Benedict"];
[recipes addObject:@"Mushroom Risotto"];
[recipes addObject:@"Full Breakfast"];
}
我能知道如何解決初始化我的NSMutableArray它殺死它?爲什麼它會在滾動時導致這樣的錯誤?採用第二種方法
編輯錯誤:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 4 beyond bounds [0 .. 3]'
*** First throw call stack:
(
0 CoreFoundation 0x01c285e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x019ab8b6 objc_exception_throw + 44
2 CoreFoundation 0x01bc94e6 -[__NSArrayM objectAtIndex:] + 246
3 TapGame 0x00002ffc -[FastestTapViewController tableView:cellForRowAtIndexPath:] + 268
4 UIKit 0x0080861f -[UITableView _createPreparedCellForGlobalRow:withIndexPath:] + 412
5 UIKit 0x008086f3 -[UITableView _createPreparedCellForGlobalRow:] + 69
6 UIKit 0x007ec774 -[UITableView _updateVisibleCellsNow:] + 2378
7 UIKit 0x007ffe95 -[UITableView layoutSubviews] + 213
8 UIKit 0x00784267 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 355
9 libobjc.A.dylib 0x019bd81f -[NSObject performSelector:withObject:] + 70
10 QuartzCore 0x0429a2ea -[CALayer layoutSublayers] + 148
11 QuartzCore 0x0428e0d4 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
12 QuartzCore 0x0428df40 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 26
13 QuartzCore 0x041f5ae6 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 294
14 QuartzCore 0x041f6e71 _ZN2CA11Transaction6commitEv + 393
15 QuartzCore 0x042c2aea _ZN2CA7Display11DisplayLink14dispatch_itemsEyyy + 474
16 QuartzCore 0x042c2f6b _ZN2CA7Display16TimerDisplayLink8callbackEP16__CFRunLoopTimerPv + 123
17 CoreFoundation 0x01be6bd6 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 22
18 CoreFoundation 0x01be65bd __CFRunLoopDoTimer + 1181
19 CoreFoundation 0x01bce628 __CFRunLoopRun + 1816
20 CoreFoundation 0x01bcdac3 CFRunLoopRunSpecific + 467
21 CoreFoundation 0x01bcd8db CFRunLoopRunInMode + 123
22 GraphicsServices 0x0361b9e2 GSEventRunModal + 192
23 GraphicsServices 0x0361b809 GSEventRun + 104
24 UIKit 0x00719d3b UIApplicationMain + 1225
25 TapGame 0x0000a3e2 main + 130
26 libdyld.dylib 0x02167725 start + 0
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Program ended with exit code: 0
你有沒有登錄'recipes'數組的值? –
適合我。你是否簡化了這篇文章的代碼? – streem
檢查滾動它 – iphonemaclover