Search code examples
iosuitableviewtableviewfavorites

Adding cell from one tableView to another


in relation to a other question, I tried a few things to add a cell from one tableView to a new one. First of all I have a little picture which shows how this process should work. enter image description here There is the CarsViewController containing an array of cars. When you tap on one cell a new view (CarDetailViewController) which shows the details of each car and has got a favoriteButton opens. By tapping on this Button the cell of this car from the tableView (CarsViewController) should be added to a new tableView (FAVViewController) as you can see in the picture.

I've already tried something but it didn't work. The Car class:

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

The CarDetailViewController with its button:

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

        [favoritesArray addObject:car];

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

        }

And finally the 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;
}

UPDATE

I`ve tried something to remove one cell from the tableView but after reloading the view all cells are away.

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

and...

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

    }
}

Solution

  • Add this to your 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;
    }
    

    In setFav method to store array

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

    And to retrieve array in favorites view controller

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

    This makes your custom objects conform to NSCoding so you can use NSUserDefaults.