0

I want that ,if the users taps a button, these tableCell should be added to a new tableView (where you can see your favorites).

I've one class Car where the content of the cars is synthesized:

#import "Car.h"
@implementation Car
@synthesize name;
@synthesize speed;
@synthesize selected;

@end

The CarsViewController contains a tableView where you can find each car:

@implementation CarsViewController {
    NSArray *cars;
}

@synthesize tableView = _tableView;

- (void)viewDidLoad
{
    Car *car1 = [Car new];
    car1.name = @"A1";
    car1.speed = @"200 km/h";
    car1.selected = FALSE;

  Car *car2 = [Car new];
    car2.name = @"A2";
    car2.speed = @"220 km/h";
    car2.selected = FALSE;

cars = [NSArray arrayWithObjects:car1, car2, nil];
}

And finally the CarDetailViewController has the favorite-button. And if you click on it the cell of these car should be added to a new ViewController (with TableView). But what should I add to the method of CarDetailViewController?

- (IBAction)setFAV:(id)sender {
    // don't know what to write here    
}

Thanks in advance for all your answers.

UPDATE

So here is a little picture which shows how it should work. enter image description here

  • Passing Data http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers – Chris Tetreault Jul 23 '13 at 13:47
  • Thanks...but I actually know how to pass data between two views, but I don't know how to adopt it to my example. And i've read that you maybe should use coreData...it would be great if you can give me a sample code as I'm very new to iOS-development. –  Jul 23 '13 at 14:30

4 Answers4

0

In this method you need an array which hold all selected cars and u need to pass this array to newView controller there u use this array to show fav cars

Shankar BS
  • 8,394
  • 6
  • 41
  • 53
0

Make your car array a NSMutableArray or create a new NSMutableArray with all your favourite cars and if the user clicks the button, add a new object to that array. Then, this array should be the data source of your tableview and after adding a new object to the NSMutableArray do

[tableview reloadData];
Heckscheibe
  • 630
  • 7
  • 8
0

Your Car class could have a BOOL isFavorite; and then on a selection in the first tableView you could set the isFavorite property for the car at the index selected to YES. Then, in your favorite table view, one option is to show only those cars in your list who have the isFavorite = YES.

Tyler Bell
  • 61
  • 7
  • Yeah...I thought of this...but when I do this (it's very easy) I don't know how to write in my favorite `tableView`...maybe you can give me an example –  Jul 23 '13 at 15:18
  • To get a better thought of what you're trying to do, let me get this straight. You have one table view with cars listed. You want to be able to tap a car and this tap adds it to a favorite list. And then you want to display this favorite list in another table view? – Tyler Bell Jul 23 '13 at 15:31
  • Almost...you should not tap on the cell...but at a button which is located in the view, which opens when you tab on the cell...but the rest is right: you add it to a favorite list and display this list in a new tableView –  Jul 23 '13 at 15:36
  • I'm quite confused still on how you want the interactions to work. I know what code to write for this to happen, but I'm not sure where to tell you in your code to place it because I do not understand the interactions. Could you paste a screenshot into your question for my reference? – Tyler Bell Jul 23 '13 at 15:45
  • So...I hope you understand me better now ;) –  Jul 23 '13 at 16:30
0

There are many ways to do this, and since you haven't specified when or where you're going to be using your Favorites Table View, one solution that might work for you is to use NSUserDefaults.

When the user hits the favorite button (Assuming you've already created and initialized an array)

[favoritesArray addObject:Car1];

[[NSUserDefaults standardUserDefaults] setObject:favoritesArray forKey:@"favoriteCars"];

And then when you need to access this array to use as the data source for your Favorites TableView in another view controller:

NSMutableArray *array = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"favoriteCars"]];
Chris Tetreault
  • 1,973
  • 13
  • 19
  • But how do I have access on car1,car2,etc. in `CarDetailViewController` when it's only created in `CarsViewController`? Apart from that this sounds quite good –  Jul 23 '13 at 19:10
  • When what is only created in CarsViewController? – Chris Tetreault Jul 23 '13 at 19:26
  • the objects car1 and car2 are created in `carsViewController`, e.g. `Car *car1 = [Car new];`but how can I use:`[favoritesArray addObject:car1];` then in `CarDetailViewController`? XCODE tells me that car1 is a undeclared variable –  Jul 23 '13 at 19:30
  • You would need to pass the Car selected from the table view in carsViewController to your CarDetailViewController – Chris Tetreault Jul 23 '13 at 19:31
  • ok...now I don't get it...can you please show me a example code? –  Jul 23 '13 at 19:33
  • What are you misunderstanding? The link I provided as a comment above explains perfectly how to pass data (in this case your car object) to the next view controller – Chris Tetreault Jul 23 '13 at 19:41
  • Ah...I think I know what you mean...in the `CarDetailViewController` the information for the selected car/tableCell is displayed...so there is a label which show the speed of each car. But I don't have access to the `cars`array which is created in `CarsViewController` –  Jul 23 '13 at 19:43
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/34013/discussion-between-codeinorange-and-user2242550) – Chris Tetreault Jul 23 '13 at 19:43