c# - TableView Cells Have Their Data Reset On Scrolling -


-----edit-----

i've found workaround problem, utilising draggingstarted method inside tableviewsource. not optimal solution, , continue looking proper way deal problem, now, can continue on process.

public override void draggingstarted(uiscrollview scrollview) {     (int = 0; < 12; i++)     {         try         {             globalsupport.newclientdata[i] = ((scrollview uitableview).cellat(nsindexpath.fromitemsection(i, 0)).contentview.subviews[1] uitextfield).text;         }         catch         {         }     }      console.writeline("hoi"); } 

in getcell method, search new data structure, find right value:

if (globalsupport.newclientdata[indexpath.row] != "") {     cell.cellvalue = globalsupport.newclientdata[indexpath.row]; } 

-----endedit-----

i working on implementing tableview-focused app. means of screens tableviews, have re-usable cells in them. these cells defined using xcode designer (this means have .cs, .xib , .designer in resources each different unique cell).

when building tableview pre-defined data (custom data objects, dictionaries, lists, etc.) call methods in cell's .cs modify 2 uilabels (or uilabel , uiimageview) defined in .xib. goes well; data supplied gets put labels , other controls, via getcell method inside tableviewsource, , remains there, after scrolling (since data pre-defined, , uses indexpath.row determine put).

now comes trouble:

i have cell, has uilabel , uitextfield in it. uilabel has text modified correspond localizable strings file. have switch in getcell method, inside tableviewsource, looks this:

public override uitableviewcell getcell(uitableview tableview, nsindexpath indexpath) {     ctextinput cell = (ctextinput)tableview.dequeuereusablecell(ctextinput.identifier);      switch (indexpath.row)     {         case 0:             cell.celllabel = nsbundle.mainbundle.localizedstring("txtbsn", "", "");             break;         case 1:             cell.celllabel = nsbundle.mainbundle.localizedstring("txtprefix", "", "");             break;         case 2:             cell.celllabel = nsbundle.mainbundle.localizedstring("txtinitials", "", "");             break;         case 3:             cell.celllabel = nsbundle.mainbundle.localizedstring("txtfirstname", "", "");             break;         case 4:             cell.celllabel = nsbundle.mainbundle.localizedstring("txtlastname", "", "");             break;         case 5:             cell.celllabel = nsbundle.mainbundle.localizedstring("txtdateofbirth", "", "");             cell.accessory = uitableviewcellaccessory.detaildisclosurebutton;             break;         case 6:             cell.celllabel = nsbundle.mainbundle.localizedstring("txtcity", "", "");             break;         case 7:             cell.celllabel = nsbundle.mainbundle.localizedstring("txtstreet", "", "");             break;         case 8:             cell.celllabel = nsbundle.mainbundle.localizedstring("txthousenumber", "", "");             break;         case 9:             cell.celllabel = nsbundle.mainbundle.localizedstring("txtzipcode", "", "");             break;         case 10:             cell.celllabel = nsbundle.mainbundle.localizedstring("txtcountry", "", "");             break;         case 11:             cell.celllabel = nsbundle.mainbundle.localizedstring("txtphonenumber", "", "");             break;         default:             break;     }      return cell; } 

this works great.

now, when view's loaded, user can input or data in uitextfield, next each of these uilabels. data gets supplied, , user progresses down tableview, text stays there... until user gets bottom of screen, , needs scroll view other cells. happens then, data supplied in first few cell's uitextfield gets reset value, specified in cell's .xib file, "". because of dequeuereusablecell in beginning of getcell method. getcell method being called every time new cell enters user's view. means cells disappear user's view, reset default.

supply data

scrolling down

scrolling up

i don't want separate set of .cs, .xib , .designer files each cell in view, , have found no method save contents of cell's uitextfield of yet. what's more, need content of uitextfield inside every cell, in order create new object, need further progress proces.

any on subject appreciated. if need more info, don't hesitate contact me!

dear regards, björn

the datasource uitableview needs provide cell data needs render , correctly. since cells reused, can thought of "dumb" , rely on data source know display.

keeping in mind, need update datasource include whatever inputted uitextfield cell.

here example of how accomplish this:

1.) implement data source uitableview key/value pairs:

datasource = new dictionary<string, string> () {     { "label 1", string.empty },     { "label 2", string.empty },     ...     ... }; 

for scenario "label 1" nsbundle.mainbundle.localizedstring("txtbsn", "", "")

2.) implement ctextinput cell this:

public partial class ctextinput : uitableviewcell {     action<string, string> ontextfieldtextchanged;      public customcell ()     {     }      public customcell (intptr handle) : base (handle)     {     }      public void setupcell (string labeltext, string value, action<string, string> ontextfieldtextchanged)     {         celllabel.text = labeltext;         celltextfield.text = value;         ontextfieldtextchanged = ontextfieldtextchanged;          celltextfield.editingdidend += handleeditingdidend;     }      void handleeditingdidend (object sender, eventargs e)     {         var textfield = sender uitextfield;         ontextfieldtextchanged (celllabel.text, textfield.text);     }      // clean     public override void prepareforreuse ()     {         base.prepareforreuse ();          ontextfieldtextchanged = null;         celltextfield.editingdidend -= handleeditingdidend;     }      protected override void dispose (bool disposing)     {         if (disposing) {             ontextfieldtextchanged = null;         }          base.dispose (disposing);      }  } 

the important things here setupcell method , handleeditingdidend event handler. hook editingdidend of uitextfield can invoke passed in ontextfieldtextchanged action.

3.) add ontextfieldtextchange handler , implement getcell method in view controller this:

public override uitableviewcell getcell (uitableview tableview, foundation.nsindexpath indexpath) {     var cell = tableview.dequeuereusablecell ("cellidentifier")  ctextinput ?? new ctextinput ();      var labeltext = datasource.keys.elementat (indexpath.row);     var textfieldtext = datasource.values.elementat (indexpath.row);      cell.setupcell (labeltext, textfieldtext, ontextfieldtextchange);      return cell; }   public void ontextfieldtextchange (string key, string value) {     if (datasource.containskey (key)) {         datasource [key] = value;     } } 

here define ontextfieldtextchangemethod can passed in action<string, string> ontextfieldtextchanged parameter in setupcell method defined earlier in ctextinput class.

in ontextfieldtextchange method update value given key in datasource.

now when uitextfield emits editingdidend event, data source updated include value user entered.

hope helps!


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -