i new ios coding, looked around here in , tried several approaches solve this, nothing seems work. i'm shure it's because of silly mistake of mine :-)
i have sectioned table, 25 rows in total, divided in 2 sections. there switch each cell.accessoryview, try store switch state, when changes, in nsarray, logging array content @ index weirdly returns 0 in case. (the array instantiated property (nonatomic, readwrite) of tableviewcontroller, , synthesized)
and , when scroll table , down, switches return in default off state.
thanks help!
- (void)viewdidload { [super viewdidload]; //countries dict , switch state array init.. self.countries = [nsdictionary dictionarywithcontentsoffile:[[nsbundle mainbundle] pathforresource:@"countries" oftype:@"plist"]]; nsnumber *b = [nsnumber numberwithbool:no]; (int i=0; i<210; i++) { [statiswitch addobject:b]; nslog(@"%d", [[statiswitch objectatindex:i] integervalue]); } //tableview drawing.. - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *cellidentifier = @"cellwithswitch"; uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier]; if (cell == nil) { cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier]; } nsstring *continent = [self tableview:tableview titleforheaderinsection:indexpath.section]; nsstring *country = [[self.countries valueforkey:continent] objectatindex:indexpath.row]; //simple tag calculation... nsinteger tagob = indexpath.section * 100 + indexpath.row; uiswitch *myswitch = [[uiswitch alloc] initwithframe:cgrectzero]; [myswitch addtarget:self action:@selector(switchchange:) forcontrolevents:uicontroleventvaluechanged]; myswitch.tag = tagob; if ([statiswitch count] > tagob) { [myswitch seton:[[statiswitch objectatindex:tagob] boolvalue] animated:no]; } cell.textlabel.text = country; cell.accessoryview = myswitch; return cell; } and method called @ switch status change:
- (void) switchchange:(uiswitch *)sender { nsnumber *c = [nsnumber numberwithbool:sender.on]; [statiswitch replaceobjectatindex:sender.tag withobject:c]; nslog(@"switch tag is: %i , state is: %d", sender.tag, sender.on); nslog(@"switch states array value: %d", [[statiswitch objectatindex:sender.tag] integervalue]); }
i think had omitted initialize statiswitch before adding objects it.
in viewdidload before for-loop, add line
statiswitch = [nsmutablearray array];
Comments
Post a Comment