将单元格从一个tableView添加到另一个

时间:2013-07-25 12:59:59

标签: ios uitableview tableview favorites

与其他question相关,我尝试了一些方法将一个单元格从一个tableView添加到一个新的单元格。首先,我有一张小图片,展示了这个过程应该如何运作。 enter image description hereCarsViewController包含一系列汽车。当您点击一个单元格时,会显示一个新视图(CarDetailViewController),其中显示每辆车的详细信息并打开favoriteButton。通过点击此按钮,应该将tableView(CarsViewController)中此车的单元格添加到新的tableView(FAVViewController)中,如图所示。

我已经尝试了一些东西,但它没有用。 Car类:

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

@end

CarsViewController

@implementation CarsViewController {
    NSArray *cars;
}

@synthesize tableView = _tableView;

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

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

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

CarDetailViewController及其按钮:

    @implementation CarDetailViewController{
        NSMutableArray *favoritesArray;
    }
...
    - (IBAction)setFAV:(id)sender {

        [favoritesArray addObject:car];

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

        }

最后是FAVViewController

@implementation FAVViewController{

    NSMutableArray *array;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    array = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"favoriteCars"]];


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

    }

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"Cell2";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }

    Car *cars2 = [array objectAtIndex:indexPath.row];
    UILabel *CarNameLabel = (UILabel *)[cell viewWithTag:100];
    CarNameLabel = cars2.name;

    return cell;
}

更新

我已尝试从tableView中删除一个单元格,但在重新加载视图后,所有单元格都不在了。

-(void)remove{
    [favoritesArray removeObject:car];
    [[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:favoritesArray] forKey:@"favoriteCars"];
} //in CarDetailView

和...

- (void)tableView:(UITableView *)table commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (editingStyle == UITableViewCellEditingStyleDelete)
    {

        CarDetailViewController *instance = [[CarDetailViewController alloc] init];
        [instance remove];


        [array removeObjectAtIndex:indexPath.row];
        [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

    }
}

3 个答案:

答案 0 :(得分:0)

不是将汽车添加到CarDetailViewController的favoritesArray,而是必须将它添加到FAVViewController的数组中。然后在它的tableView上调用reloadData。

您需要将数组属性移动到接口而不是实现,或者更好地创建一个可以为您执行这两个操作的方法。

答案 1 :(得分:0)

将此添加到您的Car.m

- (void)encodeWithCoder:(NSCoder *)coder;
{
    [coder encodeObject:name forKey:@"name"];
    [coder encodeObject:speed forKey:@"speed"];
}

- (id)initWithCoder:(NSCoder *)coder;
{
    self = [[Car alloc] init];
    if (self != nil)
    {
        name = [coder decodeObjectForKey:@"name"];
        speed = [coder decodeObjectForKey:@"speed"];
    }   
    return self;
}

setFav方法中存储数组

NSData *dataForArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"favoriteCars"];
if (dataForArray != nil)
{
    favoritesArray = [NSMutableArray arrayWithArray:[NSKeyedUnarchiver unarchiveObjectWithData:dataForArray]];

}

[favoritesArray addObject:Car];
[[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:favoritesArray] forKey:@"favoritesCars"];

并在收藏夹视图控制器中检索数组

NSData *dataForArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"favoriteCars"];
if (dataForArray != nil)
{
    array = [NSMutableArray arrayWithArray:[NSKeyedUnarchiver unarchiveObjectWithData:dataForArray]];

}

这使您的自定义对象符合NSCoding,因此您可以使用NSUserDefaults。

答案 2 :(得分:0)

使用SharedManager文件以完成您的要求 MyManager.h
MyManager.m

在viewdidload中创建共享管理器的实例

_sharemanager=[MyManager sharedManager];

在转到detailViewController时,将选定的Object设置为sharedManager对象为

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
        DetailViewController *detailVC=[[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
        [_sharemanager setCarDictionary:[carsArray objectAtIndex:indexPath.row]];
        [self.navigationController pushViewController:detailVC animated:YES];
}

在DetailViewController的viewDidLoad方法中创建sharedManager实例并获取所选的对象值并设置为标签(如果有的话)

- (void)viewDidLoad
{
    _shareManager=[MyManager sharedManager];

    carName.text=[[_shareManager carDictionary] valueForKey:@"name"];
    speed.text=[[_shareManager carDictionary] valueForKey:@"speed"];

    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

点击收藏夹按钮将sharedInstance的当前对象设置为sharedManager文件中定义的收藏夹数组

-(IBAction)setMyFavorite:(id)sender
{
    NSMutableArray *favArray=[_shareManager favCarsArray];

    if ([favArray indexOfObject:[_shareManager carDictionary]] == NSNotFound) {
        [favArray addObject:[_shareManager carDictionary]];
        [_shareManager setFavCarsArray:favArray];
    }
}

同样,最后在FavoriteViewController上创建sharedManager实例并获取喜欢的数组列表并重新加载收藏表: -

favoriteCarList=[_sharemanager favCarsArray];
[favTable reloadData];

DOWNLOAD SAMPLE HERE