ios - Tableview cell expandable with text field causes textfield to jump around -


so have been trying little while create table view expandable sections , 1 non expandable section. 1 of expandable sections should have 3 text fields inside them in can edit test inside text field. able working bt moment collapse section , expand again textfield duplicates above , swaps out above cell. ihavent been able figure out why or how make not that. idea when user enters text in field , selects enter text stored array.

the code:

    - (void)viewdidload{     [super viewdidload];      if (!expandedsections){         expandedsections = [[nsmutableindexset alloc] init];     }     manualsensorname = [[nsmutablearray alloc]initwithobjects: @"sensor",@"",@"2",@"t", nil]; }  - (void)didreceivememorywarning{     [super didreceivememorywarning];     // dispose of resources can recreated. }  #pragma mark - expanding  - (bool)tableview:(uitableview *)tableview cancollapsesection:(nsinteger)section{     if (section>0) return yes;      return no; }   #pragma mark - table view data source  - (nsinteger)numberofsectionsintableview:(uitableview *)tableview{     return 3; }  - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section{     if ([self tableview:tableview cancollapsesection:section])     {         if ([expandedsections containsindex:section])         {             return 5; // return rows when expanded         }          return 1; // top row showing     }      // return number of rows in section.     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:uitableviewcellstyledefault reuseidentifier:cellidentifier];     }      // configure cell...      if ([self tableview:tableview cancollapsesection:indexpath.section]){         if (!indexpath.row){             // first row             cell.textlabel.text = @"expandable"; // top row showing              if ([expandedsections containsindex:indexpath.section])             {                 uiimageview *arrow = [[uiimageview alloc] initwithimage:[uiimage imagenamed:@"arrowup.png"]];                 cell.accessoryview = arrow;             }             else             {                 uiimageview *arrow = [[uiimageview alloc] initwithimage:[uiimage imagenamed:@"arrowdown.png"]];                 cell.accessoryview = arrow;             }         }         else {             if (indexpath.row == 1){                 nsstring *flightnumtext = [manualsensorname objectatindex:indexpath.row];                 cell.textlabel.text = flightnumtext;             }             else if (indexpath.row == 2){                 txtmanualsensor = [[uitextfield alloc] initwithframe:cgrectmake(180, 5, 120, 30)];                 txtmanualsensor.placeholder = @"select";                 txtmanualsensor.delegate = self;                 txtmanualsensor.autocorrectiontype = uitextautocorrectiontypeno;                 txtmanualsensor.backgroundcolor = [uicolor whitecolor];                 [txtmanualsensor setborderstyle:uitextborderstylebezel];                 [txtmanualsensor settextalignment:nstextalignmentcenter];                 [txtmanualsensor setreturnkeytype:uireturnkeydone]; //                uitextfield *playertextfield = [[uitextfield alloc] initwithframe:cgrectmake(180, 5, 120, 30)]; //                playertextfield.adjustsfontsizetofitwidth = yes; //                playertextfield.textcolor = [uicolor blackcolor]; //                playertextfield.placeholder = @"sample"; //                playertextfield.tag = 200; //                playertextfield.delegate = self; //                [cell.contentview addsubview:playertextfield];                 cell.textlabel.text = @"sensor name";                 [cell addsubview:txtmanualsensor];             }             else if (indexpath.row == 3){                 cell.textlabel.text = @"some detail";                 cell.accessoryview = nil;                 cell.accessorytype = uitableviewcellaccessorydisclosureindicator;             }          }     }     else {         cell.accessoryview = nil;         cell.textlabel.text = @"normal cell";      }      return cell; } - (bool)textfieldshouldendediting:(uitextfield *)textfield {     [manualsensorname replaceobjectatindex:2 withobject:textfield.text];     return yes; } -(bool) textfieldshouldreturn:(uitextfield *)textfield{      [textfield resignfirstresponder];     return yes; }  // override support conditional editing of table view. - (bool)tableview:(uitableview *)tableview caneditrowatindexpath:(nsindexpath *)indexpath {     // return no if not want specified item editable.     return yes; }   #pragma mark - table view delegate  - (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath{      if ([self tableview:tableview cancollapsesection:indexpath.section]){         if (!indexpath.row){             [tableview beginupdates];              // first row toggles exapand/collapse             [tableview deselectrowatindexpath:indexpath animated:yes];              nsinteger section = indexpath.section;             bool currentlyexpanded = [expandedsections containsindex:section];             nsinteger rows;              nsmutablearray *tmparray = [nsmutablearray array];              if (currentlyexpanded) {                 rows = [self tableview:tableview numberofrowsinsection:section];                 [expandedsections removeindex:section];             }             else {                 [expandedsections addindex:section];                 rows = [self tableview:tableview numberofrowsinsection:section];             }              (int i=1; i<rows; i++) {                 nsindexpath *tmpindexpath = [nsindexpath indexpathforrow:i insection:section];                 [tmparray addobject:tmpindexpath];             }              uitableviewcell *cell = [tableview cellforrowatindexpath:indexpath];             if (currentlyexpanded) {                 uiimageview *arrow = [[uiimageview alloc] initwithimage:[uiimage imagenamed:@"arrowdown.png"]];                 [tableview deleterowsatindexpaths:tmparray                                  withrowanimation:uitableviewrowanimationfade];                 cell.accessoryview = arrow;             }             else {                 uiimageview *arrow = [[uiimageview alloc] initwithimage:[uiimage imagenamed:@"arrowup.png"]];                 [tableview insertrowsatindexpaths:tmparray                                  withrowanimation:uitableviewrowanimationfade];                 cell.accessoryview =  arrow;             }             nslog(@"tableview row %ld in section %ld",(long)indexpath.row,(long)indexpath.section);             [tableview endupdates];         }         [tableview deselectrowatindexpath:indexpath animated:yes];         nslog(@"selected row %ld in section %ld",(long)indexpath.row,(long)indexpath.section);         if (indexpath.row == 1) {             // update text fields in cell table view         }     } } 

it may simple replacing uitableviewrowanimationtop uitableviewrowanimationfade:
when changing indexes in didselectrowatindexpath, uitableviewcells change physical location (remember uitableview uiscrollview), , scroller can't keep track of intent is.

uitableviewrowanimationtop attempts adjust scrolling location, fails.


other design considerations:

  • do not mix model (the array of data displayed) view model (the ui displaying model). in didselectrowatindexpath, should first re-order model, apply cells

  • consider not changing indexes on fly: may prefer model reflects view structure, i.e. tree.

  • have noticed not respecting - (void)tableview:(uitableview *)tableview , using self tableview:tableview or self.customtableview in same method? should use tableview passed you.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -