views:

36

answers:

1

I have a navigation controller based app. My views consist of two tableviews laid out like this:

Category
    Item within category

Basically i allow users to create the categories using the + button on the navigation bar. Then they select a category, and can then press the + button again to create items in that category.

My problem is if i create a category, and add some items, then go back up and choose a different category, the same items from the first category are displayed.

This is what i use to create my item controllers in didSelectRow:

if (detailViewController==nil) 
     detailViewController = [[ItemViewController alloc] init];

 detailViewController.category = [[APP_DELEGATE listsArray] objectAtIndex:indexPath.row];
 [self.navigationController pushViewController:detailViewController animated:YES];

From viewDidLoad in ItemViewController:

items = [[NSMutableArray alloc] initWithCapacity:30];

How can i stop the same items being displayed for each?

Thanks

EDIT:

Code that populates items:

- (void)addNameController:(AddName *)addNameController didAddName:(NSString *)name {

if (name) {
    NSLog(@"%@", name);
    [items addObject:name];
    [self.tableView reloadData];
}
[self dismissModalViewControllerAnimated:YES];
}
+1  A: 

Move the initialization of the items array to viewDidAppear in ItemViewController and call reloadData. The viewDidLoad is only getting called the first time ItemViewController is alloc'd and pushed.

aBitObvious
This causes the item to flash briefly in the table view but then disappears. Thanks
joec
Can you show the code that populates items? Is anything else done in viewDidAppear?
aBitObvious
see above. thanks
joec
How is that code called in viewDidAppear of the ItemViewController and why does it call dismissModal?
aBitObvious
It isnt called in viewDidAppear. Only the instantiation of items and reloadData occur... It calls dismissModal because that is how i get the data for the items array from a modal view... and according to the View Controller Programming Guide, parent view controllers should use delegation to dismiss the child modal view.
joec
Is items array declared only in ItemViewController? Is addNameController method in ItemViewController? How and when is addNameController called?
aBitObvious
The items array is defined only in ItemViewController yes. The addNameController method is defined in ItemViewController, a delegate method called from the modal view, when its Done button is clicked.
joec
By "the modal view", I assume you mean some 3rd view controller you show from the ItemViewController when user presses + button. Why is this addNameController method being called when you push the ItemViewController from the didSelectRow in the CategoryViewController? After user is finished looking at the items for category "A" and they go back to the Category table view, where is the list of items for category "A"? Is the items array in the detail view controller storing the items for ALL categories or just the category that's set before you push the detail view controller?
aBitObvious
Yes that is what i meant by "modal view". The addNameController method is only called when the user pushes Done on the modal view, not when didSelectRow is called. I would like a new instance of items to hold the items for each category seperately i think...
joec
I'd suggest storing the complete list of items for all categories in an NSMutableDictionary declared in the AppDelegate (like the listsArray) or in a singleton (or persisted on the device some other way if the lists are going to be huge). The dictionary keys could be the category names and the object for each key would be an NSArray of strings (Items). Regardless, the ItemViewController will need to refresh its tableview in its viewDidAppear to show the correct items when it is presented.
aBitObvious
Yes i agree. How would be the best way to implement storing items? So using a dictionary, after I create the category, i now do this: add an object to the dictionary (NSNull) with the category as the key, because there are currently no items. Then when i load the ItemViewController, add the item to a temp array before replacing that NSNull, with the temp array. But now each time the view refreshes only the most recent item added shows up. The key point i think is this intermediate array. I really appreciate your help :)
joec
When you create a category, don't add null. Instead, add an empty NSMutableArray (not NSArray). You can alloc+init it with capacity 1 but just don't don't add anything to it. In the ItemViewController, you don't need a temp/intermediate array. Reference the array for the current category directly from the dictionary. Although you can declare a local reference to it for easier manipulation. Something like `NSMutableArray *itemsForCat = (NSMutableArray *)[dict objectForKey:currentCategory];`.
aBitObvious
Thank you. It works! Im so glad we got that cleared up. :)
joec