Domanda

I am created a table view in my storyboard using following code.

#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

But it kills while scrolling up showing following EXEC_BAD_ACCESS Error enter image description here

If i initialise my NSMutableArray using add object see below then it works fine

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Initialize table data


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

    [recipes addObject:@"Full Breakfast"];


}

Can i know how to fix it?And why it causes such error while scrolling?

EDIT ERROR using second method :

*** 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
È stato utile?

Soluzione

If you're initializing your array as

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

it's like, you're declaring your array with autorelease.

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

you don't know at which point, the array will be released.

So you better stick to your second solution.

or else try with @property way of declaring NSMutablearray.

Altri suggerimenti

The NSMutableArray recipes is probably deallocated from memory and you're attempting to use it again.

You need to retain recipes:

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

Using initWithObjects will directly initialize an array, so it won't be autoreleased; you'll need to include [recipes release]; in dealloc.

Also you could use:

return [self.recipes count];

or simply:

[self.recipes count];
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top