ios - Hide TableView Row without removing object array -


ive searched , came many different approaches accomplish hiding row in table view. none seem work , may way wrote it, not sure. last example tried adjusting height , when ran project rows in 1 row.

what im trying accomplish when landing on view controller , bool value set false hide row not remove array object because when user presses send values button send values entire array though 1 row may hidden.

#import "sensorvaluesystem.h"  @interface sensorvaluesystem ()  @end   @implementation sensorvaluesystem  - (void)viewdidload{     [super viewdidload];      valuearray=[[nsmutablearray alloc]initwithobjects:[nsnumber numberwithint:0],[nsnumber numberwithint:0],[nsnumber numberwithint:0], nil];     labels = [nsmutablearray arraywithobjects:@"temp sensor", @"humid sensor", @"uv sensor", nil];  }   -(uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath {     static nsstring *cellidentifier = @"cell";     uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier];     if (cell == nil) {         cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier];          uilabel *label = [[uilabel alloc]initwithframe:cgrectmake(10.0f, 10.0f, 140.0f, 21.0f)];         [cell addsubview:label];         [label settag:456];          uistepper *stepper = [[uistepper alloc]initwithframe:cgrectmake(200.0f, 10.0f, 20.0f, 20.0f)];         [cell addsubview:stepper];         [stepper settag:123];         [stepper addtarget:self action:@selector(stepperchanged:) forcontrolevents:uicontroleventvaluechanged];     }     [cell settag:indexpath.row];     int count = [[valuearray objectatindex:indexpath.row] intvalue];      [(uistepper*)[cell viewwithtag:123] setvalue:count]; //    [(uilabel*)[cell viewwithtag:456] settext:[nsstring stringwithformat:@"%@: %d", @"stepper", count]];     [(uilabel*)[cell viewwithtag:456] settext:[nsstring stringwithformat:@"%@:   %d", [labels objectatindex:indexpath.row], count]];       return cell; }   - (void)stepperchanged:(uistepper*)sender {     int row = [sender.superview tag];     int value = (int)[sender value];     nslog(@"stepper name: %@ @ row: %d = %d",[labels objectatindex:row], row,value);      [valuearray replaceobjectatindex:row withobject:[nsnumber numberwithint:value]];      [(uilabel*)[(uitableviewcell *)sender.superview viewwithtag:456] settext:[nsstring stringwithformat:@"%@:   %d", [labels objectatindex:row], value]]; }   - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)component {     return [labels count];  }  - (ibaction)btnpressed:(id)sender{     nslog(@"values of array: %@", valuearray);     nslog(@"value of temp: %@", [valuearray objectatindex:0]);     nslog(@"value of humid: %@", [valuearray objectatindex:1]);     nslog(@"value of uv: %@", [valuearray objectatindex:2]);  }  @end 

header file

#import <uikit/uikit.h>  @interface sensorvaluesystem : uiviewcontroller{ nsmutablearray *valuearray;     nsmutablearray *labels;     int passengers;     int bags;     int child;     bool hidetemp;     bool hidehumid;     bool hideuv; }  @property (strong, nonatomic) iboutlet uitableview *tableview; - (ibaction)btnpressed:(id)sender;  @end 

keeping table data in array convenient thing do, it's not requirement. in case might simpler separate out special value , keep in own pointer. need 2 bookkeeping variables: boolean tells if row hidden, , index of special row.

@interface tablecontroller ()  @property (strong, nonatomic) specialvalueclass * specialrowvalue;  @end  // ivar/property if needs change. static const nsuinteger kspecialrowindex = 3;  @implementation tablecontroller {     bool _specialrowisshown; } 

then, array of other values set up, have test bool know how many rows table view has:

- (nsinteger)tableview:(uitableview *)table numberofrowsinsection:(nsinteger)section {     return [valuearray count] + (_specialrowisshown ? 1 : 0); } 

configuring individual cells having separate pointer special value make life easier. instead of array index arithmetic, use special value if you're on special row, , array otherwise:

- (uitableviewcell *)tableview:(uitableview *)table cellforrowatindexpath:(nsindexpath *)indexpath {     // retrieve/create cell...      nsuinteger row = [indexpath row];     if( _specialrowisshown && (row == kspecialrowindex) ){         // configure cell using data in specialrowvalue     }     else {         // configure cell using data in valuearray[row]     }      return cell; } 

Comments