ios - Add new UITableView row with custom text -


using code

- (ibaction)testadd:(id)sender  {     nsindexpath *indexpath = [nsindexpath indexpathforrow:self.numberofrows insection:0];      [self.tableview beginupdates];     self.numberofrows++;     [self.tableview insertrowsatindexpaths:@[indexpath]withrowanimation:uitableviewrowanimationbottom];     [self.tableview endupdates];   } 

i'm able add new item tableview via 'add' button on app. adds item identical item on table preceded it. example, have tableview first row displaying string "test", hitting add adds row displays "test". able pass in custom value new row, hitting add outputs row "newthing".

- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath {     uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:@"uitableviewcell" forindexpath:indexpath];      // configure cell...      cell.textlabel.text = self.val2;      return cell; } 

my data source view controller takes user inputs , sends tabelviewcontroller, text item "val2". want achieve ability hit add, go user input view controller, new data , send tableviewcontroller displayed

what you're asking, kinda stuff done in -cellforrowatindexpath: (most of times, depends on way have designed datasource) if doesn't matter you, can do:

- (ibaction)testadd:(id)sender  {     nsindexpath *indexpath = [nsindexpath indexpathforrow:self.numberofrows                                                 insection:0];     self.numberofrows++;      [self.tableview beginupdates];     [self.tableview insertrowsatindexpaths:@[indexpath]                           withrowanimation:uitableviewrowanimationbottom];     [self.tableview endupdates];      uitableviewcell *cell = [self.tableview cellforrowatindexpath:indexpath];     [cell.textlabel settext:@"newthing"]; } 

but note when scroll far up/down , return cell, show "test" (that's -cellforrowatindexpath: show it's true purpose)

ps: include -cellforrowatindexpath: method implementation in question if want proceed further


edit:

your -cellforrowatindexpath static... in sense sets self.val2 cell.textlabel.
lets start 10 rows, -cellforrowatindexpath called 10 times , every time, set self.val2 onto current cell's textlabel.
now... when add 1 row (on button tap), -cellforrowatindexpath called 11th cell , same* text set it.
*this technically happened changed cell's text

basically, tableview doesn't know how differentiate between existing cell , new added cell because datasource not dynamic.

to direct tableview on how handle different cells, need create more dynamic datasource.

there different approaches use i'd way:

- (void)viewdidload {     [super viewdidload];      self.val2 = @"test";      //declare "nsmutablearray *arrdatasource;" globally     //this soul of tableview     arrdatasource = [[nsmutablearray alloc] init];      int i_numberofcells = 10;      //populate beginning cells default text     (int = 0; < i_numberofcells; i++) {         nsmutabledictionary *dictionary = [[nsmutabledictionary alloc] init];         [dictionary setobject:self.val2 forkey:@"displaytext"];          [arrdatasource addobject:dictionary];     } }  - (nsinteger)numberofsectionsintableview:(uitableview *)tableview {     return 1; }  - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section {     //return number of objects in arrdatasource     return arrdatasource.count; }  - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath {     uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:@"uitableviewcell" forindexpath:indexpath];      //pick value key "displaytext" , set onto cell's label     [cell.textlabel settext:arrdatasource[indexpath.row][@"displaytext"]];     //this dynamic in nature because can modify contents     //of arrdatasource , tell tableview update appropriately      return cell; }  -(void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath {     //make indexpath of new cell created     nsindexpath *indexpathnext = [nsindexpath indexpathforitem:arrdatasource.count insection:0];      //add appropriate contents dictionary     nsmutabledictionary *dictionary = [[nsmutabledictionary alloc] init];     [dictionary setobject:@"newthing" forkey:@"displaytext"];      //add dictionary object main array datasource     [arrdatasource addobject:dictionary];      //add tableview     [self.tableview beginupdates];     [self.tableview insertrowsatindexpaths:@[indexpathnext]                           withrowanimation:uitableviewrowanimationautomatic];     [self.tableview endupdates];     //this ends calling -cellforrowatindexpath newly created cell     //-cellforrowatindexpath shows text (you put in dictionary in method above) } 

ps: -cellforrowatindexpath: called whenever cell updates or refreshes or needs displayed , method needs implemented properly


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -