gpt4 book ai didi

ios - 带有 NSMutableArray 的表的奇怪行为

转载 作者:行者123 更新时间:2023-11-29 03:05:11 25 4
gpt4 key购买 nike

我使用以下代码在 Storyboard中创建了一个表格 View 。

#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 错误 enter image description here

如果我使用添加对象初始化 NSMutableArray(见下文),那么它工作正常

- (void)viewDidLoad
{
[super viewDidLoad];

// Initialize table data


NSMutableArray *recipes = [[NSMutableArray alloc] init];
[recipes addObject:@"Egg Benedict"];
[recipes addObject:@"Mushroom Risotto"];

[recipes addObject:@"Full Breakfast"];


}

我可以知道如何修复它吗?为什么它在滚动时会导致这样的错误?

使用第二种方法编辑错误:

*** 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

最佳答案

如果您将数组初始化为

 [NSMutableArray arrayWithObjects:@"Egg Benedict", @"Mushroom Risotto", @"Full Breakfast", @"Hamburger", @"Ham and Egg Sandwich", nil];

这就像,您正在使用 autorelease 声明您的数组。

 [[[NSMutableArray alloc] initWithObjects:@"Egg Benedict", @"Mushroom Risotto", @"Full Breakfast", @"Hamburger", @"Ham and Egg Sandwich", nil] autorelease];

你不知道什么时候数组会被释放。

所以你最好坚持你的第二个解决方案

或者尝试使用 @property 声明 NSMutablearray 的方式。

关于ios - 带有 NSMutableArray 的表的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22856640/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com