objective c - Delete row from UITableView when I have multiple sections iOS (values are in Dynamic NSMutableDictionary) -


let me describe scenario first.

i have nsmutabledictionary called self.worddic, have key & value this: (this dictionary dynamic, not fixed)

key     value ---------------------------------------------------------       (apple, aim, arise, attempt, airplane, absolute) b       (bubble, bite, borrow, basket) c       (cat, correct) d       (dog, direction, distribute) 

code:

- (void)viewdidload {     [super viewdidload];      if ([self.wordlistarray count] != 0)     {         self.worddic = [self sorteddictionary:self.wordinwordlistarray];          // sorted key array         self.keyarray = [[nsarray alloc] init];         nsarray *key = [[nsarray alloc] init];         key = [self.worddic allkeys];         self.keyarray = [key sortedarrayusingselector:@selector(localizedcaseinsensitivecompare:)];     } } 

i have nsarray called keyarray have of nsdictionary keys (alphabetically). , in cellforrowatindexpath show values of particular key (alphabetically).

please take have tableview methods :

my tableview:

-(nsinteger)numberofsectionsintableview:(uitableview *)tableview {     return [self.keyarray count]; }  -(cgfloat)tableview:(uitableview *)tableview heightforheaderinsection:(nsinteger)section {     return 28; }  -(uiview *)tableview:(uitableview *)tableview viewforheaderinsection:(nsinteger)section {     uiview *view = [[uiview alloc] initwithframe:cgrectmake(0, 0, tableview.frame.size.width, 28)];     // add title label     uilabel *titlelabel = [[uilabel alloc] initwithframe:cgrectmake(10, 5, 100, 18)];     [titlelabel setfont:[uifont fontwithname:@"helvetica-bold" size:14.0f]];     nsstring *string = [self.keyarray objectatindex:section];      [titlelabel settext:string];     [view addsubview:titlelabel];      return view; }  - (cgfloat)tableview:(uitableview *)tableview heightforrowatindexpath:(nsindexpath *)indexpath {     return khometableviewcellheight; }  - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section {     nsstring *key = [self.keyarray objectatindex:section];     nsmutablearray *value = [self.worddic objectforkey:key];      return [value count]; }  - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath {     static nsstring *cellidentifier = khometableviewcellid;     hometableviewcell *cell = (hometableviewcell *)[self.hometableview dequeuereusablecellwithidentifier:cellidentifier];      if (!cell)     {         cell = [[hometableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier];     }     cell.selectionstyle = uitableviewcellselectionstylenone;      nsstring *sectitle = [self.keyarray objectatindex:indexpath.section];     secdata = [self.worddic objectforkey:sectitle];     [secdata sortusingselector:@selector(localizedcaseinsensitivecompare:)];     nsstring *data = [secdata objectatindex:indexpath.row];     [cell.wordlabel settext:data];      return cell; } 

everything works perfectly. want delete rows.

- (void)tableview:(uitableview *) tableview commiteditingstyle:(uitableviewcelleditingstyle)editingstyle forrowatindexpath:(nsindexpath *)indexpath {     //other code     ..............     ..............       // remove info tableview array     [self.whicharraywillbehere removeobjectatindex:indexpath.row];     [self.mytableview deleterowsatindexpaths:[nsarray arraywithobject:indexpath] withrowanimation:uitableviewrowanimationfade];     } } 

in commiteditingstyle array should have replace self.whicharraywillbehere array? cause, first have no section in method , secondly, load rows, alphabetically. how can know array, should used here , it's indexpath?? if understand problem please reply.

thanks lot in advance.
have day. :)

i think you're close - have keyarray sorted , stored in property. can values array dictionary - did in data source methods. quick , dirty way delete right row/word sort again.

-(void)tableview:(uitableview *) tableview commiteditingstyle:(uitableviewcelleditingstyle)editingstyle forrowatindexpath:(nsindexpath *)indexpath {   // other code   // ..   // remove info tableview array    nsstring *key = [self.keyarray objectatindex:indexpath.section];   nsmutablearray *words = [self.worddic objectforkey:key];   [words sortusingselector:@selector(localizedcaseinsensitivecompare:)];    [words removeobjectatindex:indexpath.row];   [self.mytableview deleterowsatindexpaths:[nsarray arraywithobject:indexpath] withrowanimation:uitableviewrowanimationfade]; } 

again quick solution, note sort array(s) each time row deleted , anytime table reloaded (but if word arrays small may not matter - that's you)


Comments