objective c - iOS UItableView remove object from array by index path -
i have custom contact book sorted a-z sections. trying add array selected contacts
- (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath { nsmutabledictionary *contactinfo = [nsmutabledictionary new]; cell *cell = (cell *)[self.conttableview cellforrowatindexpath:indexpath]; //nslog(@"cell %@", cell.contact.fullname); if (!cell.contact.contactchecked) { cell.contactimage.image = [uiimage imagenamed:@"cell_blue_circle.png"]; cell.contact.contactchecked = yes; //nslog(@"did select %@", cell.contact.fullname); nslog(@"index checked row %d section %d", indexpath.row, indexpath.section); [contactinfo setvalue:cell.contact.fullname forkey:@"name"]; [contactinfo setvalue:cell.contact.numbers.firstobject forkey:@"phone"]; [self.seletedpeople insertobject:contactinfo atindex:indexpath.row]; } else { nslog(@"index unchecked row %d section %d", indexpath.row, indexpath.section); cell.contactimage.image = [uiimage imagenamed:@"cell_gray_circle.png"]; cell.contact.contactchecked = no; [self.seletedpeople removeobjectatindex:indexpath.row]; } nslog(@"dict selected %@", self.seletedpeople);
}
what happens, in cell app crashing error
* terminating app due uncaught exception 'nsrangeexception', reason: '* -[__nsarraym insertobject:atindex:]: index 1 beyond bounds empty array' *** first throw call stack: (0x29c02fef 0x38150c8b 0x29b1cf8f 0xf7fe9 0x2d36e56b 0x2d41d43b 0x2d2d2a91 0x2d24d38f 0x29bc8fed 0x29bc66ab 0x29bc6ab3 0x29b13201 0x29b13013 0x313f2201 0x2d2b7a59 0x10c075 0x386dcaaf) libc++abi.dylib: terminating uncaught exception of type nsexception
update:
- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *cellid = @"cell"; cell *cell = [tableview dequeuereusablecellwithidentifier:cellid forindexpath:indexpath]; if (searchresults) { //nslog(@"cell %@", cell.contact.fullname); contact = [searchresults objectatindex:indexpath.row]; cell.contact = contact; cell.firstnamelabel.text = contact.fullname; cell.avatar.image = contact.image; cell.avatar.layer.bordercolor = [uicolor graycolor].cgcolor; cell.avatar.layer.borderwidth = 0.5; cell.avatar.layer.cornerradius = 25.0; cell.avatar.layer.maskstobounds = yes; cell.number.text = contact.numbers.firstobject; } else { nsstring *sectiontitle = [[[namesdictionary allkeys] sortedarrayusingselector:@selector(localizedcaseinsensitivecompare:)] objectatindex:indexpath.section]; nsarray *sectioncontacts = [namesdictionary objectforkey:sectiontitle]; contact = [self getcontactfromarray:[sectioncontacts objectatindex:indexpath.row]]; cell.firstnamelabel.text = [sectioncontacts objectatindex:indexpath.row]; cell.avatar.image = contact.image; cell.avatar.layer.bordercolor = [uicolor graycolor].cgcolor; cell.avatar.layer.borderwidth = 0.5; cell.avatar.layer.cornerradius = 25.0; cell.avatar.layer.maskstobounds = yes; cell.number.text = contact.numbers.firstobject; cell.contact = contact; cell.tag = indexpath.row; } if (contact.contactchecked) { cell.contactimage.image = [uiimage imagenamed:@"cell_blue_circle.png"]; } else { cell.contactimage.image = [uiimage imagenamed:@"cell_gray_circle.png"]; } return cell;
}
the way use in such cases. create model class , load tableview models. when select cell or deselect cell. add model in array. after when de select selected cell, can same model indexpath.row , can use nsarray
method fetch model in selected array , remove there. fix issue can use indexpath.row key in dictionary during selection. after when deselect cell use predicate added dictionary array using store selected ones. once find delete array.