使用NSMutableArray的表的奇怪行为

时间:2014-04-04 07:46:00

标签: ios objective-c xcode scroll tableview

我使用以下代码在故事板中创建了一个表格视图。

#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

2 个答案:

答案 0 :(得分:1)

如果您要将数组初始化为

 [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];

您不知道array将在何时释放。

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

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

答案 1 :(得分:0)

NSMutableArray recipes可能已从内存中释放,您尝试再次使用它。

您需要保留 recipes

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

使用initWithObjects将直接初始化一个数组,因此它不会被自动释放;您需要在dealloc中加入[recipes release];

你也可以使用:

return [self.recipes count];

或简单地说:

[self.recipes count];