views:

307

answers:

2

Hi,

In the following bit of code, I'm setting the table view cell text with a value from the NSMutableArray 'categories' which is a property of my view controller. That works fine.

But when I try the exact same code in another method, it crashes (it compiles without errors or warnings). If I change the following line in the didSelectRowAtIndexPath method:

NSString *categoryName = [categories objectAtIndex:indexPath.row];

to

NSString *categoryName = [[NSString alloc] initWithString:@"test"];

It works... any ideas?

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

 // Configure the cell.
 NSString *categoryName = [categories objectAtIndex:indexPath.row];
 cell.textLabel.text = categoryName;

    return cell;
}




// Override to support row selection in the table view.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    printf("User selected row %d\n", [indexPath row] + 1);

 ButtonsPageViewController *bView =  [ButtonsPageViewController alloc];
 NSLog(@"created instance of buttonspageviewcontroller");

 NSString *categoryName = [categories objectAtIndex:indexPath.row];
 NSLog(@"category name set");

 bView.selectedCategory = categoryName;
 NSLog(@"selected category property set");

 [self.navigationController pushViewController:bView animated:YES];
 NSLog(@"push view controller");

 [bView release];
}
+2  A: 

The difference between

NSString *categoryName = [categories objectAtIndex:indexPath.row];

and

NSString *categoryName = [[NSString alloc] initWithString:@"test"];

Is that the first line copies a pointer to the object (retain count does not change) whereas the second one creates a new object (retain count = 1).

In cellForRowAtIndexPath, when you set the text property, it copies or retains the string, so you're fine. In didSelectRowAtIndexPath you are setting a property of ButtonsPageViewController, which I assume is your own code, but perhaps it is not copying or retaining the object.

Also, the line

ButtonsPageViewController *bView =  [ButtonsPageViewController alloc];

is going to lead to problems. You need to call init to properly initialize the object. All you've done in that line is allocate memory for it.

In general, it looks like you need to brush up on Retain/Release memory management. That should save you some trouble.

benzado
I did have bView initialising properly before, I've now initialised it. I agree, I do need to brush up on memory management!
Jamesz
I got it!You were right about the memory management. I retained the category array and it's working now.Thanks for helping out a relative noob! Kudos to you :D
Jamesz
+1  A: 

Like benzado says, it's an issue retaining the selectedCategory value in ButtonsPageViewController.

Are you using @property and @synthesize or are you writing your own accessors? If it's the former, you probably need to look at the property declaration attributes. Otherwise, it's probably a retain/release thing in your custom accessor.

The Declared Properties section of The Objective-C 2.0 Programming Laungauge is a good resource for rules of declaring synthesized accessors.

Jablair