crazyoxygen
crazyoxygen

Reputation: 716

NSMutableArray not pass value to CellForRowAtIndexPath?

I can put value into array and here is my code and prob.

Search.h

@interface SearchKeyword : UIViewController {
    UITableView * newsTable;
    UILabel * waitLabel;
    UIActivityIndicatorView *activityIndicator;
    UIView * backView;

    NSMutableArray *jsonKanji;
}

@property (nonatomic, retain) IBOutlet UITableView * newsTable;
@property (nonatomic, retain) IBOutlet UILabel * waitLabel;
@property (nonatomic, retain) IBOutlet UIActivityIndicatorView * activityIndicator;
@property (nonatomic, retain) IBOutlet UIView * backView;

- (UITableViewCell *) getCellContentView:(NSString *)cellIdentifier;

@end

and here is Search.m

- (void)viewDidAppear:(BOOL)animated{
...
jsonKanji = [NSMutableArray arrayWithArray:(NSArray *)[jsonDict valueForKey:@"kanji_name"]];
...
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSLog(@"jsonkanji 0 %@",[jsonKanji objectAtIndex:0]);
    return [jsonKanji count];
}

Here is result "jsonkanji 0 Japan" that correct and If I change objAtindex=1 It show "jsonkanji 1 England" that's correct too.

But when I go to this

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"jsonkanji 0 %@",[jsonKanji objectAtIndex:1]);
    ...
    return cell;
}

It's crash !! plase help me explain what happen ? And it show only "EXC_BAD_ACCESS" @main.m

Upvotes: 0

Views: 716

Answers (3)

extremeboredom
extremeboredom

Reputation: 1502

You have a memory management issue. [NSMutableArray arrayWithArray:] gives you an autoreleased object, as per Apple's memory management guidelines.

In your first test, you're checking the value at some point before the object is dealloc'd. By the time the delegate call from the table view has arrived the object has been dealloc'd, and the memory no longer exists.

You need to make sure to retain the array when you create it (and release it when you are finished with it).

You can do this explicitly:

- (void)viewDidAppear:(BOOL)animated{
    ...
    jsonKanji = [[NSMutableArray arrayWithArray:(NSArray *)[jsonDict valueForKey:@"kanji_name"]] retain];
    ...
}

Or create a property and assign the value using the property, which will handle the retain itself: (interface)

@interface SearchKeyword : UIViewController {
....
@property (nonatomic, retain) INSMutableArray *jsonKanji;
...
@end

(implementation)

- (void)viewDidAppear:(BOOL)animated{
    ...
    self.jsonKanji = [NSMutableArray arrayWithArray:(NSArray *)[jsonDict valueForKey:@"kanji_name"]];
    ...
}

Upvotes: 3

rckoenes
rckoenes

Reputation: 69489

You need to retain the NSMutable array, try creating a property for the array.

Add the following to your .h file

@property (nonatomic, retain) NSMutableArray *jsonKanji;

Then @synthesize the jsonKanji in the .m file. And change the loading of the array to:

- (void)viewDidAppear:(BOOL)animated{
   ...
   self.jsonKanji = [NSMutableArray arrayWithArray:(NSArray *)[jsonDict valueForKey:@"kanji_name"]];
   ...
}

Upvotes: 1

André Morujão
André Morujão

Reputation: 7143

NSMutableArray arrayWithArray: is autoreleased. You need to retain it.

Upvotes: 1

Related Questions