ios - Identifying table cells in a method -
i trying implement flatuikit in custom cell in uitableview within viewcontroller returns values nsuserdefaults based on switch state. usual switch, have method 'saveswitchstate' connected valuechanged outlet. problem in want pass state user defaults , utilize label key, method can't access the cells , labels. have tried adding:
static nsstring *cellidentifier = @"cell"; setuponecell *cell = (setuponecell *)[tableview dequeuereusablecellwithidentifier:cellidentifier forindexpath:indexpath];
to method, still no luck. if there improve way save switch states valuechanged method i'm ears. below viewcontroller , custom cell.
viewcontroller.m:
#import "setup1viewcontroller.h" #import "setup1tableview.h" #import "setuponecell.h" #import "social.h" #import "fuiswitch.h" #import "uifont+flatui.h" #import "uicolor+flatui.h" @interface setup1viewcontroller () - (ibaction)saveswitchstate:(id)sender; @end @implementation setup1viewcontroller - (id)initwithnibname:(nsstring *)nibnameornil bundle:(nsbundle *)nibbundleornil { self = [super initwithnibname:nibnameornil bundle:nibbundleornil]; if (self) { // custom initialization } homecoming self; } - (void)viewdidload { [super viewdidload]; self.onetableview.datasource = self; self.onetableview.delegate = self; self.medias = [[nsmutablearray alloc] init]; social *media = [[social alloc] init]; media.socialmedia = @"facebook"; [self.medias addobject:media]; media = [[social alloc] init]; media.socialmedia = @"twitter"; [self.medias addobject:media]; media = [[social alloc] init]; media.socialmedia = @"instagram"; [self.medias addobject:media]; media = [[social alloc] init]; media.socialmedia = @"tumblr"; [self.medias addobject:media]; media = [[social alloc] init]; media.socialmedia = @"gmail"; [self.medias addobject:media]; media = [[social alloc] init]; media.socialmedia = @"linked in"; [self.medias addobject:media]; media = [[social alloc] init]; media.socialmedia = @"github"; [self.medias addobject:media]; media = [[social alloc] init]; media.socialmedia = @"you tube"; [self.medias addobject:media]; media = [[social alloc] init]; media.socialmedia = @"vine"; [self.medias addobject:media]; media = [[social alloc] init]; media.socialmedia = @"soundcloud"; [self.medias addobject:media]; //self.setup1table.editing = yes; } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of resources can recreated. } #pragma mark - table view info source - (nsinteger)numberofsectionsintableview:(uitableview *)tableview { homecoming 1; } - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { homecoming [self.medias count]; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *cellidentifier = @"cell"; setuponecell *cell = (setuponecell *)[tableview dequeuereusablecellwithidentifier:cellidentifier forindexpath:indexpath]; cell.cellswitch.oncolor = [uicolor turquoisecolor]; cell.cellswitch.offcolor = [uicolor cloudscolor]; cell.cellswitch.onbackgroundcolor = [uicolor midnightbluecolor]; cell.cellswitch.offbackgroundcolor = [uicolor silvercolor]; cell.cellswitch.offlabel.font = [uifont boldflatfontofsize:14]; cell.cellswitch.onlabel.font = [uifont boldflatfontofsize:14]; social *media = (self.medias)[indexpath.row]; cell.socialname.text = media.socialmedia; homecoming cell; } - (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath { return; } - (ibaction)saveswitchstate:(id)sender { nsuserdefaults *defaults = [nsuserdefaults standarduserdefaults]; if ([cell.cellswitch ison]) [defaults setbool:yes forkey:celllabel]; else [defaults setbool:no forkey:celllabel]; } @end
setuponecell.h:
#import <uikit/uikit.h> #import "fuiswitch.h" #import "uifont+flatui.h" #import "uicolor+flatui.h" @interface setuponecell : uitableviewcell @property (weak, nonatomic) iboutlet uilabel *socialname; @property (weak, nonatomic) iboutlet fuiswitch *cellswitch; @end
the basic thought have cell hear switch value changing, study alter view controller through delegate call. in view controller can info associated cell , utilize modify nsuserdefaults
.
how this:
inside cell's setup code should add together cell target switch, instead of having view controller target. (probably in cell's -awakefromnib
work in sort of -configurewithsocialmedia:
method.)
- (void)awakefromnib { [self.cellswitch addtarget:self action:@selector(switchvaluechanged:) forcontrolevents:uicontroleventvaluechanged]; } - (void)switchvaluechanged:(id)sender { [self.delegate setuponecell:self witchvaluedidchange:[self.cellswitch ison]]; }
then create protocol method -setuponecell:switchvaluedidchange:
passes cell , switch value. create cell take delegate follows protocol.
code protocol:
@protocol setuponecelldelegate; @interface setuponecell : uitableviewcell @property (weak, nonatomic) iboutlet uilabel *socialname; @property (weak, nonatomic) iboutlet fuiswitch *cellswitch; @property (weak, nonatomic) id<setuponecelldelegate> delegate; @end @protocol setuponecelldelegate <nsobject> - (void)setuponecell:(setuponecell *)cell switchvaluedidchange:(bool)switchvalue; @end
now view controller set callback cell when switch value changes. when configure cell, create sure set view controller delegate:
- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *cellidentifier = @"cell"; setuponecell *cell = (setuponecell *)[tableview dequeuereusablecellwithidentifier:cellidentifier forindexpath:indexpath]; cell.cellswitch.oncolor = [uicolor turquoisecolor]; cell.cellswitch.offcolor = [uicolor cloudscolor]; cell.cellswitch.onbackgroundcolor = [uicolor midnightbluecolor]; cell.cellswitch.offbackgroundcolor = [uicolor silvercolor]; cell.cellswitch.offlabel.font = [uifont boldflatfontofsize:14]; cell.cellswitch.onlabel.font = [uifont boldflatfontofsize:14]; social *media = (self.medias)[indexpath.row]; cell.socialname.text = media.socialmedia; cell.delegate = self; homecoming cell; }
and implement delegate method:
- (void)setuponecell:(setuponecell *)cell switchvaluedidchange:(bool)switchvalue { nsindexpath *indexpath = [self.onetableview indexpathforcell:cell]; nsstring *label = [self.medias[indexpath.row] socialmedia]; nsuserdefaults *defaults = [nsuserdefaults standarduserdefaults]; [defaults setbool:switchvalue forkey:label]; }
ios objective-c uitableview nsuserdefaults uiswitch
No comments:
Post a Comment