ios - Keep selected state of UITableViewCells when scrolling -


i trying keep selected state of multiple cells on didselectrowatindexpath method. have edit button i've set loops through every cell select each field on uitableview.

here code edit button on tap selects rows.

- (ibaction)editbuttontapped:(id)sender {  (int = 0; < self.casedatatableview.numberofsections; i++) {     (nsinteger r = 0; r < [self.casedatatableview numberofrowsinsection:i]; r++) {         [self tableview:casedatatableview didselectrowatindexpath:[nsindexpath indexpathforrow:r insection:i]];     } } 

}

when calling didselectrowatindexpath method, following code.

    - (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath { okoperatienotetableviewcell *cell = (okoperatienotetableviewcell *)[self.casedatatableview cellforrowatindexpath:indexpath]; cell.cellindexpath = indexpath; [cell hidelabelandshowbuttons];} 

incase wondering here hidelabelandshowbuttons method.

- (void)hidelabelandshowbuttons { self.casedatakeylabel.hidden = no;  if (!self.disabled) {     self.casedatavaluelabel.hidden = yes;     self.textfield.hidden = no;     if ([self.inputtype isequaltostring:@"switcher"] || [self.inputtype isequaltostring:@"multiselect"] || [self.inputtype isequaltostring:@"picker"] || [self.inputtype isequaltostring:@"datepicker"] || [self.inputtype isequaltostring:@"selectcontact"]) {         self.button.hidden = no;     }else {         self.button.hidden = yes;     } }  self.casedatadescriptiontextview.hidden = yes;} 

now @ point, have rows selected. if scroll down , selection of these rows not there anymore. i'm aware when go in , out of view, cellforrowatindexpath method recreates these cells. following cellforrowatindexpath method.

- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath {  static nsstring *cellidentifier = @"casedata"; okoperatienotetableviewcell * cell = [[okoperatienotetableviewcell alloc]init];  cell = [tableview dequeuereusablecellwithidentifier:cellidentifier forindexpath:indexpath];  if (indexpath.row < _procedurevariables.count) {     if ([[[_casedataarray objectatindex:indexpath.row] valueforkey:@"key"] isequaltostring:@"procedure"]) {         [cell setlabelswithkey:[[_casedataarray objectatindex:indexpath.row] valueforkey:@"key"] andvalue:[self.model valueforkey:@"var_procedurename"]];     }else {         [cell setlabelswithkey:[[_casedataarray objectatindex:indexpath.row] valueforkey:@"key"] andvalue:[[_casedataarray objectatindex:indexpath.row] valueforkey:@"value"]];     }      okproceduretemplatevariablesmodel *variablemodel = _procedurevariables[indexpath.row];     cell.variable = variablemodel.value;     [cell showlabelandhidebuttons];     cell.delegate = self;     [cell setupcelltype]; } else if (indexpath.row == _procedurevariables.count) {     nsstring *text = [nsstring stringwithformat:@"%@ \n\n %@", [_templatedictionary objectforkey:@"indicationtext"], [_templatedictionary objectforkey:@"proceduretext"] ];     [cell showdescription:text];     nslog(@"cell.casedatadescriptiontextview.font.fontname = %@", cell.casedatadescriptiontextview.font.fontname); }  cell.procedureid = _procedureid; [tableview setcontentinset:uiedgeinsetsmake(1.0, 0.0, 0.0, 0.0)];  return cell; 

}

i'm trying figure out how keep selected state of these cells once cellforrowatindexpath method called. suggestions welcomed.

i tried simulate situation, created customcell , saved indexpaths of selectedrows in custom selectedpaths mutable array(initialized in viewdidload). after every click removed or added related indexpath array. worked case. hope helps.

-(uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath {     static nsstring *cellidentifier = @"casedata";     notableviewcell *cell = (notableviewcell *)[tableview dequeuereusablecellwithidentifier:cellidentifier];     if (cell == nil)     {         nslog(@"new cell created row %d", (int)indexpath.row);         cell = [[[nsbundle mainbundle] loadnibnamed:@"notableviewcell" owner:self options:nil] objectatindex:0];     }     if ([selectedpaths indexofobject:indexpath] != nsnotfound) // cell in selected state.      {         [cell.textlabel settext:@"this cell selected"];//selected state job.         return cell;     }         [cell.textlabel settext:[nsstring stringwithformat:@"%d", (int)indexpath.row]];     return cell; } -(void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath {      if ([selectedpaths indexofobject:indexpath] != nsnotfound) {         [selectedpaths removeobject:indexpath];     }     else{         [selectedpaths addobject:indexpath];     }     //[tableview reloaddata];      [tableview reloadrowsatindexpaths:@[indexpath] withrowanimation:uitableviewrowanimationnone];//instead of reloading reload clicked cell. } 

Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -