Initialize NSMutableArray
Im very new in xcode 3 and i really need help for this
I developed my apps using UITableView and XML to showed the content.
i had 3 .xib files which it rootViewController , SecondViewController and mainview.
So the problem is: When i try to executed didSelectrow in rootViewController and access the NSMutableArray *array in SecondViewController and replace the *array value with new array value in rootViewController before pushed animation.
The array value on my SecondViewController was changed for the first time开发者_高级运维 but when i pushed the back button and select the other row, my SecondViewController array kept read the previous array not change to a new one. I try to initialize but no luck
This is my code on rootViewController UITableview (didSelectRow):
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if(2ndController == nil)
2ndController = [[DetailViewController alloc] initWithNibName:@"SecondViewController" bundle:[NSBundle mainBundle]];
//Declare xml NSMutable array record
ListRecord *record = [self.entries objectAtIndex:indexPath.row];
//access SecondViewController NSMutable *record
2ndController.record = [[[NSMutableArray alloc] init] autorelease];
//inserting the value from firstview to secondview before push
2ndController.record = record;
//push animation
[self.navigationController pushViewController:2ndController animated:YES];
}
This is my second view controller :
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
switch(indexPath.section)
{
case 0:
[cell setText:record.name];
break;
case 1:
[cell setText:record.Age];
break;
case 2:
[cell setText:record.summary];
break;
}
return cell;
}
Hope someone can help me..
Thanks in advance.....
Few things,
You do,
2ndController.record = [[[NSMutableArray alloc] init] autorelease];
and follow it up with
[cell setText:record.name];
Clearly, the record
property doesn't seem to be an instance of NSMutableArray
so I think the array initialization part is incorrect as you do, and already mentioned,
2ndController.record = record;
But the problem I think is that you are retaining your UITableViewController
subclass. Have you tried reloading the data?
[self.tableView reloadData];
Add it in the viewWillAppear
method of your DetailViewController
.
You should look at these two lines again:
2ndController.record = [[[NSMutableArray alloc] init] autorelease];
//inserting the value from firstview to secondview before push
2ndController.record = record;
The first line doesn't do anything useful for you. It creates and initializes a new NSMutableArray and sets the record property to that new array.
But then in the very next line you set the same 'record' property again to a different object and so that array in the first line is no longer referenced. So you might as well not have ever created it.
That's not your issue exactly, but this comment was too big for a comment. :)
精彩评论