Featured post
iphone - Set switch to On, reload table then insert a new row -
i've got uitableviewcontroller use create settings of application.
there section 1 row put uiswitch.
how can insert new row inside same section of row switch if switch in set yes? , how can delete row if switch set no?
can me? thanks!
i tried use insertrowsatindexpaths:withrowanimation: method doesn't work...
settings table code:
- (void)viewdidload { [super viewdidload]; self.title = nslocalizedstring(@"impostazioni", @""); } - (void)viewwillappear:(bool)animated { [self.tableview reloaddata]; } -(void)addcelltosetcode:(id)sender { if ([codeswitch ison]) { nsindexpath *updatedindexpath = [nsindexpath indexpathforrow:1 insection:2]; [self.tableview beginupdates]; [self.tableview insertrowsatindexpaths:[nsarray arraywithobject:updatedindexpath] withrowanimation:uitableviewrowanimationtop]; [self.tableview endupdates]; [[nsuserdefaults standarduserdefaults] setbool:codeswitch.on forkey:@"stateofswitch"]; } else { nsindexpath *updatedindexpath = [nsindexpath indexpathforrow:1 insection:2]; [self.tableview beginupdates]; [self.tableview deleterowsatindexpaths:[nsarray arraywithobject:updatedindexpath] withrowanimation:uitableviewrowanimationtop]; [self.tableview endupdates]; [[nsuserdefaults standarduserdefaults] setbool:codeswitch.on forkey:@"stateofswitch"]; } } #pragma mark - #pragma mark table view data source - (nsinteger)numberofsectionsintableview:(uitableview *)tableview { return 4; } -(nsstring *)tableview:(uitableview *)tableview titleforheaderinsection:(nsinteger)section { if (section == 0) { return nslocalizedstring(@"listadesideri", @""); } if (section == 1) { return nslocalizedstring(@"condivisionemail", @""); } if (section == 2) { return @"sicurezza"; } else { return nil; } } - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { if (section == 0) || (section == 2) || (section == 3) { return 2; } else if (section == 1) { return 1; } else { return 1; } } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *cellidentifier = @"cell"; uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier]; if (cell == nil) { cell = [[[uitableviewcell alloc] initwithstyle:uitableviewcellstylevalue1 reuseidentifier:cellidentifier] autorelease]; } if (indexpath.section == 0 && indexpath.row == 0) { nsuserdefaults *defaults = [nsuserdefaults standarduserdefaults]; cell.textlabel.text = nslocalizedstring(@"ordine", @""); if ([[defaults objectforkey:@"ordinaper"] isequaltostring:@"nome"]) { cell.detailtextlabel.text = nslocalizedstring(@"nome", @""); } if ([[defaults objectforkey:@"ordinaper"] isequaltostring:@"costo"]) { cell.detailtextlabel.text = nslocalizedstring(@"costo", @""); } if ([[defaults objectforkey:@"ordinaper"] isequaltostring:@"categoria"]) { cell.detailtextlabel.text = nslocalizedstring(@"categoria", @""); } if ([[defaults objectforkey:@"ordinaper"] isequaltostring:@"nome discendente"]) { cell.detailtextlabel.text = nslocalizedstring(@"nomediscendente", @""); } if ([[defaults objectforkey:@"ordinaper"] isequaltostring:@"costo discendente"]) { cell.detailtextlabel.text = nslocalizedstring(@"costodiscendente", @""); } if ([[defaults objectforkey:@"ordinaper"] isequaltostring:@"categoria discndente"]) { cell.detailtextlabel.text = nslocalizedstring(@"categoriadiscendente", @""); } cell.accessorytype = uitableviewcellaccessorydisclosureindicator; } if (indexpath.section == 0 && indexpath.row == 1) { nsuserdefaults *defaults = [nsuserdefaults standarduserdefaults]; cell.textlabel.text = nslocalizedstring(@"dettaglidesiderio", @""); if ([[defaults objectforkey:@"dettagliview"] isequaltostring:@"costoview"]) { cell.detailtextlabel.text = nslocalizedstring(@"costo", @""); } if ([[defaults objectforkey:@"dettagliview"] isequaltostring:@"descrizioneview"]) { cell.detailtextlabel.text = nslocalizedstring(@"descrizione", @""); } if ([[defaults objectforkey:@"dettagliview"] isequaltostring:@"urlview"]) { cell.detailtextlabel.text = nslocalizedstring(@"url", @""); } cell.accessorytype = uitableviewcellaccessorydisclosureindicator; } if (indexpath.section == 1 && indexpath.row == 0) { nsuserdefaults *defaults = [nsuserdefaults standarduserdefaults]; cell.textlabel.text = nslocalizedstring(@"shortener", @""); if ([[defaults objectforkey:@"linkshortener"] isequaltostring:@"nessuno"]) { cell.detailtextlabel.text = nslocalizedstring(@"nessuno", @""); } if ([[defaults objectforkey:@"linkshortener"] isequaltostring:@"is.gd"]) { cell.detailtextlabel.text = nslocalizedstring(@"is.gd", @""); } if ([[defaults objectforkey:@"linkshortener"] isequaltostring:@"bit.ly"]) { cell.detailtextlabel.text = nslocalizedstring(@"bit.ly", @""); } if ([[defaults objectforkey:@"linkshortener"] isequaltostring:@"tinyurl"]) { cell.detailtextlabel.text = nslocalizedstring(@"tinyurl", @""); } if ([[defaults objectforkey:@"linkshortener"] isequaltostring:@"linkyy"]) { cell.detailtextlabel.text = nslocalizedstring(@"linkyy", @""); } cell.accessorytype = uitableviewcellaccessorydisclosureindicator; } if (indexpath.section == 2 && indexpath.row == 0) { cell.textlabel.text = @"access code"; codeswitch = [[uiswitch alloc] initwithframe:cgrectmake(0, 0, 84, 27)]; cell.accessoryview = codeswitch; [codeswitch addtarget:self action:@selector(addcelltosetcode:) forcontrolevents:uicontroleventvaluechanged]; codeswitch.on = [[nsuserdefaults standarduserdefaults] boolforkey:@"codeswitchstate"]; cell.selectionstyle = uitableviewcellselectionstylenone; } if (indexpath.section == 3 && indexpath.row == 0) { cell.textlabel.text = nslocalizedstring(@"supporto", @""); cell.accessorytype = uitableviewcellaccessorydisclosureindicator; } if (indexpath.section == 3 && indexpath.row == 1) { cell.textlabel.text = nslocalizedstring(@"informazioni", @""); cell.accessorytype = uitableviewcellaccessorydisclosureindicator; } return cell; }
edit: updates below...
i solved part of problem!
tried use [self.tableview reloaddata] doesn't work , casually solved using [self.tableview setneedsdisplay]...
now switch works if set on , go out app, closing it, switch doesn't work... how can solve this?
if can other, these pieces of code updated:
-(void)addcelltosetcode:(id)sender { if ([codeswitch ison]) { nsindexpath *updatedindexpath = [nsindexpath indexpathforrow:1 insection:2]; [self.tableview beginupdates]; [self.tableview insertrowsatindexpaths:[nsarray arraywithobject:updatedindexpath] withrowanimation:uitableviewrowanimationtop]; [self.tableview endupdates]; [self.tableview setneedsdisplay]; [[nsuserdefaults standarduserdefaults] setbool:codeswitch.on forkey:@"codeswitchstate"]; } else { nsindexpath *updatedindexpath = [nsindexpath indexpathforrow:1 insection:2]; [self.tableview beginupdates]; [self.tableview deleterowsatindexpaths:[nsarray arraywithobject:updatedindexpath] withrowanimation:uitableviewrowanimationtop]; [self.tableview endupdates]; [self.tableview setneedsdisplay]; [[nsuserdefaults standarduserdefaults] setbool:codeswitch.on forkey:@"codeswitchstate"]; } } // tableview:numberofrowsinsection: else if (section == 2) { return codeswitch.on ? 2 : 1; } // tableview:cellforrowatindexpath: if (indexpath.section == 2 && indexpath.row == 0) { cell.textlabel.text = @"access code"; codeswitch = [[uiswitch alloc] initwithframe:cgrectmake(0, 0, 84, 27)]; cell.accessoryview = codeswitch; [codeswitch addtarget:self action:@selector(addcelltosetcode:) forcontrolevents:uicontroleventvaluechanged]; codeswitch.on = [[nsuserdefaults standarduserdefaults] boolforkey:@"codeswitchstate"]; cell.selectionstyle = uitableviewcellselectionstylenone; } if (indexpath.section == 2 && indexpath.row == 1) { if ([codeswitch ison]) { cell.textlabel.text = @"set access code"; cell.accessorytype = uitableviewcellaccessorydisclosureindicator; } }
at least part of problem (in updated code) don't create uiswitch until create cell. codeswitch
ivar may end pointing different switch table row comes in , out of view.
here's how i'd this: in tableview:numberofrowsinsection:
, use nsuserdefaults see state table should in, instead of using state of switch (which may not exist yet). then, in switch's action method, call setbool:forkey:
user defaults before insert or delete table row.
in essence, makes code follow mvc model better, because separates view (the uiswitch) model (the bool in user defaults), controller (the view controller) in middle. confounding view , model (the switch , boolean state), end problems when trying deal state when view isn't available yet.
btw, shouldn't need call setneedsdisplay
on table view @ all.
- Get link
- X
- Other Apps
Comments
Post a Comment