Question

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

    }
}
Was it helpful?

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.

OTHER TIPS

Rather than adding the car to the CarDetailViewController's favoritesArray, you must add it to the FAVViewController's array. Then you call reloadData on it's tableView.

You'll need to move the array property to the interface rather than the implementation, or better create a method that can do both of these for you.

Use SharedManager file in order to complete your requirement.
MyManager.h
MyManager.m

Create an instance of shared Manager in viewdidload as

_sharemanager=[MyManager sharedManager];

While going to detailViewController set selected Object to sharedManager object as

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

On DetailViewController's viewDidLoad method create sharedManager instance and get selected Object Values and set to labels if any defined as

- (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.
}

And on click of favorite button set current object of sharedInstance to favorite array defined in sharedManager file as

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

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

Similarly, At last on FavoriteViewController make sharedManager Instance and get favorite array list and reload favorite table as:-

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

DOWNLOAD SAMPLE HERE

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top