开发者

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


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"]];
    ...
}


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


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"]];
   ...
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜