ios - NSFetchResultController with UICollectionView issue with indexes/cells on update and delete action -


first time i'm using uicollectionview, , i'm having difficulties. updating , deleting cells (will focus on delete here, came cause), data nsfetchresultcontroller. have made custom cell in in interface builder part of storyboard this:

enter image description here

i have custom uicollectionviewcellsubclass following properties:

@property (strong, nonatomic) iboutlet uibutton *deletebutton; @property (strong, nonatomic) iboutlet uitextfield *textfield; @property (strong, nonatomic) iboutlet uiview *containerview; @property (strong, nonatomic) iboutlet uiview *textfieldcontainer; 

in ib have set cell class custom class, connected elements properties of custom class , set identifier cell.

in collection view view controller set collection view , fetchresultcontroller , relevant methods this:

- (nsinteger)numberofsectionsincollectionview:(uicollectionview *)collectionview {     return [[self.fetchedresultscontroller sections] count]; }  -  (nsinteger)collectionview:(uicollectionview *)collectionview numberofitemsinsection:(nsinteger)section {     id <nsfetchedresultssectioninfo> sectioninfo = [self.fetchedresultscontroller sections][section];     return [sectioninfo numberofobjects]; }  - (uicollectionviewcell *)collectionview:(uicollectionview *)collectionview cellforitematindexpath:(nsindexpath *)indexpath {     notecollectionviewcell* cell = (notecollectionviewcell *)[collectionview dequeuereusablecellwithreuseidentifier:@"cell" forindexpath:indexpath];     [self configurecell:cell atindexpath:indexpath];     return cell; }  - (void)configurecell:(notecollectionviewcell *)cell atindexpath:(nsindexpath *)indexpath {     cell.deletebutton.tag = indexpath.row;     [cell.deletebutton addtarget:self action:@selector(deletenote:)  forcontrolevents:uicontroleventtouchupinside];      [...]      // i'm having weird problem this, se description below.      note *note = [self.fetchedresultscontroller objectatindexpath:indexpath];     cell.textfield.tag = indexpath.row;     cell.textfield.delegate = self;     cell.textfield.text = note.name;     [...]  #pragma mark - fetched results controller  - (nsfetchedresultscontroller *)fetchedresultscontroller {     [default frc method] }  - (void)controller:(nsfetchedresultscontroller *)controller didchangeobject:(id)anobject    atindexpath:(nsindexpath *)indexpath forchangetype:(nsfetchedresultschangetype)type   newindexpath:(nsindexpath *)newindexpath {     uicollectionview *collectionview = self.collectionview;      switch(type) {         case nsfetchedresultschangeinsert:             [collectionview insertitemsatindexpaths:@[newindexpath]];             break;          case nsfetchedresultschangedelete:             [collectionview deleteitemsatindexpaths:@[indexpath]];             break;         case nsfetchedresultschangeupdate:             [self configurecell:(notecollectionviewcell *)[collectionview cellforitematindexpath:indexpath] atindexpath:indexpath];             break;     } } 

my delete action looks this:

- (void)deletenote:(uibutton *)sender {     note *note = [self.fetchedresultscontroller objectatindexpath:[nsindexpath  indexpathforitem:sender.tag insection:0]];     [[appdelegate sharedappdelegate].managedobjectcontext deleteobject:note];     [[appdelegate sharedappdelegate] savecontext]; } 

my update action (uitextfield delegate method) looks this:

- (void)textfielddidendediting:(uitextfield *)textfield {     note *note = [self.fetchedresultscontroller objectatindexpath:[nsindexpath indexpathforitem:textfield.tag insection:0]];     note.name = textfield.text;     [[appdelegate sharedappdelegate] savecontext]; } 

the problem(s) follows:

  • the delete button don't delete right cells/object. , sometime object/cell won't deleted @ all.
  • sometime app crashes when delete object (sigarbt error).
  • sometimes text shows in wrong textfield.
  • sometimes when delete row 0 text, add press add, new cell added same text value previous (deleted) cell.

any ideas on how solve these problems great!

update

as comment below, log in deletenote action returns 0 indexpath row. don't know if cause.

nsindexpath *indexpath = [nsindexpath indexpathforitem:sender.tag insection:0]; nslog(@"indexpath: %@. row: %d", indexpath, indexpath.row); 

i don't know if explains problem, call

[cell.deletebutton addtarget:self action:@selector(deletenote:)  forcontrolevents:uicontroleventtouchupinside]; 

each time in configurecell:atindexpath:, multiple actions attached button if cell modified or reused. deletenote: called more once 1 touch event.

as workaround, check cell.deletebutton.tag see if action has been attached button, or better:

  • add action in notecollectionviewcell class local action in class when cell created,
  • use delegation forward action collection view controller.

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -