ios - Weird behavior when reloading cells containing textFields in dynamic tableView -


i have dynamic tableview custom cells each contain textfield , label. want following behavior:


a user taps "add" button, , table inserts cell @ index 0, cell's textfield becoming first responder.

when user finishes inputting data, one-lined textfield resigns first responder , hides itself.

the cell's label becomes visible, filled user input, , auto-resizes it's height accommodate input. cell resized @ time too.


i have code first time cell added, when more cells added gets weird. second added cell s textfield not become first responder , filled text previous textfield. subsequent added cells appear blank, except every 6 blank cells, first cell's text shows up.

what's going on, , how can fix this?

edit: @rayfleck, have resolved issue of second added cell being filled text previous textfield, second cell gets added (and subsequent added cells) still isn't becoming first responder.

here's demo showing behavior:

enter image description here

here's code:

#define font_size 16.0f #define cell_content_width 300.0f // change different size! #define cell_content_margin_width 10.0f #define cell_content_margin_height 20.0f  @interface viewcontroller ()  @property (strong) iboutlet uitableview *tableview; @property (strong) nsmutablearray *notesarray;  @end  @implementation viewcontroller  - (void)viewdidload {     [super viewdidload];      self.notesarray = [nsmutablearray arraywithobjects:@"testo 1", @"this long string of text show how cell resizes fit variable height label.", @"testo 3", @"testo 4", @"testo 5", nil]; }  - (nsinteger)numberofsectionsintableview:(uitableview *)tableview {      // return number of sections.     return 1; }  - (ibaction)addbuttontapped {      nsmutablearray *indexpaths = [nsmutablearray array];     [indexpaths addobject:[nsindexpath indexpathforrow:0 insection:0]]; // insert @ first index      [self.tableview setediting:yes animated:no]; // go edit mode cell has label hidden , textfield visible      // insertion     [self.notesarray insertobject:@"" atindex:0]; // placeholder when user starts typing     [self.tableview beginupdates];     [self.tableview insertrowsatindexpaths:indexpaths withrowanimation:uitableviewrowanimationtop];     [self.tableview endupdates];      [self.tableview setediting:no animated:no]; }  -(bool)textfieldshouldreturn:(uitextfield *)textfield {      nsindexpath *path = [nsindexpath indexpathforrow:0 insection:0]; // first table cell     hc_notecell *cell = (hc_notecell *)[self.tableview cellforrowatindexpath:path];      cell.notetextfield.hidden = yes;     cell.notelabel.text = cell.notetextfield.text;     cell.notelabel.hidden = no;      [self.notesarray replaceobjectatindex:0 withobject:cell.notetextfield.text];      [textfield resignfirstresponder];      // need reload rows cell height can recalculated     [self.tableview beginupdates];     [self.tableview reloadrowsatindexpaths:@[path] withrowanimation:uitableviewrowanimationautomatic];     [self.tableview endupdates];      return yes; }  - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section {      return self.notesarray.count; }  - (cgfloat)tableview:(uitableview *)tableview heightforrowatindexpath:(nsindexpath *)indexpath {      nsstring *text;     text = [self.notesarray objectatindex:indexpath.row];      cgsize constraint = cgsizemake(cell_content_width - (cell_content_margin_width * 2), 20000.0f);      uifont *font = [uifont fontwithname:@"helvetica neue" size:font_size];     nsdictionary *attrsdictionary = [nsdictionary dictionarywithobject:font                                                                 forkey:nsfontattributename];     nsattributedstring *attributedstring = [[nsattributedstring alloc] initwithstring:text attributes:attrsdictionary];      cgrect paragraphrect = [attributedstring boundingrectwithsize:constraint options:(nsstringdrawinguseslinefragmentorigin|nsstringdrawingusesfontleading) context:nil];      cgfloat height = max(paragraphrect.size.height, 22.0f);      return height + (cell_content_margin_height * 2);  }  - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath {     static nsstring *cellidentifier = @"notecell";      hc_notecell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier];     if (cell == nil) {         cell = [[hc_notecell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier];     }      if (self.tableview.isediting) {          cell.notelabel.hidden = yes;         cell.notetextfield.hidden = no;         cell.notetextfield.delegate = self;          [cell.notetextfield becomefirstresponder];      } else {          cell.notelabel.hidden = no;         cell.notetextfield.hidden = yes;         cell.notetextfield.delegate = self;          cell.notelabel.text = [self.notesarray objectatindex:indexpath.row];     }      return cell; }  @end 

if reusing/dequeing cells, in cellforrowatindexpath, must set new value every subview of cell, because contain contents of previous cell.

so cell.notelabel.text = nil; in cases, , in else case, set it's value array.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -