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