ios - How to get UITextField (subview to UITableViewCell) text value when tableview cell became invisible -


i created custom uitableviewcell class embedded uitextfield each cell, in additemtableviewcontroller, want text values within uitextfield-embededd cells , create new model object, i'm running problem:

cellforrowatindexpath returns nil invisible cells, after scrolled down buttom of tableview hit add button, first few rows' textfield text value became null.

is there anyway can fix this? i've been googlging hours , still not find answer it.

here's additemtableviewcontroller code:

    - (ibaction)doneadd:(uibarbuttonitem *)sender {         [self.delegate additem:[self newitem]];     }      - (nsmutablearray *)newitem     {         nsmutablearray *newitem = [[nsmutablearray alloc] init];          (int = 0; < [_appdelegate.title count]; ++) {             nsindexpath *indexpath = [nsindexpath indexpathforrow:i insection:0];             upfeditableuitableviewcell *cell = (upfeditableuitableviewcell *)[self.tableview cellforrowatindexpath:indexpath];             nslog(@"%@", cell.editfield.text);             //[newitem addobject:cell.editfield.text]; //this not work null cannot added array         }         nslog(@"%@", newitem);         return newitem;     } 

here's custom uitableviewcell class implementation

    #import "upfeditableuitableviewcell.h"      @implementation upfeditableuitableviewcell      - (id)initwithstyle:(uitableviewcellstyle)style reuseidentifier:(nsstring *)reuseidentifier     {         self = [super initwithstyle:style reuseidentifier:reuseidentifier];         if (self) {             self.editfield = [[uitextfield alloc] initwithframe:cgrectzero];             [self.contentview addsubview:self.editfield];         }         return self;     }      - (void)layoutsubviews     {         if ([self.detailtextlabel.text length] == 0) {             self.detailtextlabel.text = @" ";         }          [super layoutsubviews];          // place edit field in same place detail text field, give max width         self.editfield.frame = cgrectmake(self.detailtextlabel.frame.origin.x, self.detailtextlabel.frame.origin.y, self.contentview.frame.size.width-self.detailtextlabel.frame.origin.x, self.detailtextlabel.frame.size.height);     }      - (void)showeditingfield:(bool)show     {         self.detailtextlabel.hidden = yes;         self.editfield.text = self.detailtextlabel.text;     }       @end 

i think made fundamental mistake, have view talks model layer, lesson learned...

anyway, managed work out solution, in short, here's did:

  • made cell delegate of uitextfield
  • implemented textfielddidchange, capture textfield changes, once there's change, submit changed content model

and here's code:

in cellforrowatindex:

    [cell.editfield addtarget:self action:@selector(textfielddidchange:) forcontrolevents:uicontroleventeditingchanged];      cell.editfield.delegate = self; 

and here's code textfielddidchange:

    - (void)textfielddidchange :(uitextfield *)thetextfield     {         nsindexpath *indexpath = [self.tableview indexpathforselectedrow];         [self.item removeobjectatindex:indexpath.row];         [self.item insertobject:thetextfield.text atindex:indexpath.row];     } 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -