ios - Adding cell from one tableView to another -
in relation other question, tried few things add cell 1 tableview new one. first of have little picture shows how process should work. there carsviewcontroller
containing array of cars. when tap on 1 cell new view (cardetailviewcontroller
) shows details of each car , has got favoritebutton
opens. tapping on button cell of car tableview (carsviewcontroller
) should added new tableview (favviewcontroller
) can see in picture.
i've tried didn't work. 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
button:
@implementation cardetailviewcontroller{ nsmutablearray *favoritesarray; } ... - (ibaction)setfav:(id)sender { [favoritesarray addobject:car]; [[nsuserdefaults standarduserdefaults] setobject:favoritesarray forkey:@"favoritecars"]; }
and 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 remove 1 cell tableview after reloading view cells 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]; } }
add 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 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 retrieve array in favorites view controller
nsdata *dataforarray = [[nsuserdefaults standarduserdefaults] objectforkey:@"favoritecars"]; if (dataforarray != nil) { array = [nsmutablearray arraywitharray:[nskeyedunarchiver unarchiveobjectwithdata:dataforarray]]; }
this makes custom objects conform nscoding can use nsuserdefaults.
Comments
Post a Comment