Question

I'm using a plist with an array of dictionaries to populate a TableView and a DetailView for selected cell in TableView (prototype cell). I want to add selected dictionary to favorites tab (another TableView) when the button for that is pushed. This is my code so far:

Code so far in the DetailViewController:

-(IBAction)FavoriteButton:(id)sender
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ItemSelected"
                                                    object:selectedObject];


}

Code so far to catch the objects in FavoritesViewController:

[[NSNotificationCenter defaultCenter] addObserverForName:@"ItemSelected"
                                                  object:nil
                                                   queue:[NSOperationQueue mainQueue]
                                              usingBlock:^(NSNotification* notif) {
                                              [favoritedObjects release];
                                              favoritedObjects = [[notif object] retain];
                                              [self.tableView reloadData];
                                          }];

//And populate the TableView with the objects:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 {
 return [favoritedObjects count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Favcell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

// Configure the cell...

NSString *cellValue = [favoritedObjects valueForKey:@"Name"];
cell.textlabel.text = cellValue;

return cell;
}

This shows 20 cells with the value from the Key: "Name" from the last Object in the DetailViewController I pressed "Add to Favorites" button for. For example if I add "Gato Negro" to favorites, it displays "Gato Negro" in 20 cells. if I then add "Luna" to fav, it replaces "Gato Negro" with "Luna" in the 20 cells in FavoritesViewController.. So how can I display them one by one in the favorites TableView?

And how can I make the NotificationCenter save the changes when the app is closed, so it remembers the favorites to next time?

It seems like it's kind of a communication problem here somewhere.

Was it helpful?

Solution

You are posting the notification with the object selectedObject (which I assume is your dictionary). When you receive that notification, you are replacing your existing favorites with this object:

favoritedObjects = [[notif object] retain];

So in fact favoritedObjects now is the single selectedObject dictionary. This dictionary probably has 20 entries, so that's why you end up with 20 cells.

You need to make (at least) two changes for your code to work:

  1. Change that line above to add the notification object to your favoritedObjects array (I am assuming that this is an NSMutableArray), instead of replacing favoritedObjects with the selectedObject dictionary. You will also need to create an instance of favoritedObjects once (either on-demand when you first want to add something to it, e.g. check for favoritedObjects == nil and if it's nil, create a new NSMutableArray, or create it in a suitable place such as viewDidLoad (at some point you'll probably be saving and loading the favorites, too, so putting its creation it into one of the initialization methods is a good idea anyway, even if you're not ready for persistent favorites yet).

  2. In tableView:cellForRowAtIndexPath: make use of the indexPath that is passed to you to actually get the correct entry from your favoritedObjects array.

Also, your tableView:cellForRowAtIndexPath: is missing the code to create a new cell if there is no cell available to be dequeued. Perhaps you have just omitted this piece of code for your question here? In case you haven't, you'll need to add that code.

It is also a good idea to start method names with a lowercase letter (-(IBAction)favoriteButton:(id)sender instead of -(IBAction)FavoriteButton:(id)sender, or even better: -(IBAction)favoriteButtonPressed:(id)sender) because that's what everyone expects Objective-C methods to look like :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top