I am developing an iphone app for a class project and am displaying a bunch of different products. I am trying to create a favorites page where users can add one of the products to their favorites page. The app is set up with a bunch of different tableviews to display the different products along with their piture, name, price, and description. I want the user to either click on my addtofavorites button I will add to each of the table view cells or I was wandering if I can just use the didSelectRowAtIndexPath method in the tableviews to add all that information to the favorites table view. Thank you
1 Answers
First, look into following the MVC pattern, a good introduction: https://developer.apple.com/library/mac/#documentation/General/Conceptual/DevPedia-CocoaCore/MVC.html
That being said, I'd have a products UITableView, in a view controller. The user can then select the view controller and in the didSelectRow:
method, find out which product was selected to sou can pass the data through a delegate patterns (other ways include target-action, NSNotification, etc.) for example like this: (good example of passing data: Passing Data between View Controllers) to a separate view controller which would hold your favorites table view. Make sure the model is separate from both the products view controller and favorites view controller so you can save all the name, price, etc whatever properties you want to save.
perhaps all of this would be contained in a UITabBarController
so it's easier for the user to go back and forth between products and favorites.
-
I have seen that before and it is helpful but the problem is because the favorites view controller is a table view how do I make sure that the products I am adding will add another row to the favorites table – user2321816 Apr 26 '13 at 01:43
-
make an NSMutableArray that holds all the favorites that have been selected, then in `cellForRowAtIndexPath:` do `[whateverArrayYouMake objectAtIndex:indexPath.row];` to get the content out, and then `[self.tableView reloadData]` and set `numberOfRowsInSection:` to return whateverArrayYouMake.count that way the number of rows in the tableview will always be the same as the number of rows in the array that holds the favorite products – july Apr 26 '13 at 02:35
-
Right I have made an array inside the product classes to hold that data but I am still having problems sharing that array across multiple classes. Or how do I make the array in the favorites view while people are adding items to the favorites table – user2321816 Apr 26 '13 at 05:27
-
If you want to share your data across multiple view controllers look into using a singleton pattern :(http://stackoverflow.com/questions/6324732/using-a-singleton-to-create-an-array-accessible-by-multiple-views) -- and how are you pushing the data to the favorites view controller in the first place? – july Apr 26 '13 at 22:24