通过点击按钮将tableViewCell添加到新的TableView中 [英] Add a tableViewCell to a new TableView by tapping a button

查看:123
本文介绍了通过点击按钮将tableViewCell添加到新的TableView中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要的是,如果用户点击一个按钮,则应该将这些tableCell添加到新的tableView中(您可以在其中查看您的收藏夹).

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

我有一个Car类,其中汽车的内容是合成的:

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

CarsViewController包含一个tableView,您可以在其中找到每辆车:

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

最后,CarDetailViewController具有收藏夹"按钮. 如果单击它,则应将这些汽车的单元格添加到新的ViewController(带有TableView)中. 但是我应该在CarDetailViewController的方法中添加什么?

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

UPDATE

所以这是一张小图,显示了它应该如何工作.

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

推荐答案

执行此操作的方法有很多,并且由于您尚未指定何时何地使用收藏夹表视图,因此一种解决方案可能对您有用的是使用NSUserDefaults.

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

然后当您需要访问此数组以用作另一个视图控制器中的收藏夹" TableView的数据源时:

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

这篇关于通过点击按钮将tableViewCell添加到新的TableView中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆