Add a tableViewCell to a new TableView by tapping a button

倖福魔咒の 提交于 2019-12-13 06:13:35

问题


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.


回答1:


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




回答2:


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];



回答3:


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.




回答4:


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"]];


来源:https://stackoverflow.com/questions/17811921/add-a-tableviewcell-to-a-new-tableview-by-tapping-a-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!