ios - Adding Cells to existing UICollectionView (mutating method sent to immutable object) -


i trying find way add cells collection , keeps giving me "mutating method sent immutable object" error. not know why. posted below code working with.

    self.artistarray = [responseobject objectforkey:@"data"];     self.paginationarray = [responseobject objectforkey:@"pagination"];     //nslog(@"%@", self.paginationarray);      if(self.firstrequest){         [self.collectionview reloaddata];         self.firstrequest = false;     }     else{         nsarray *newdata = [[nsarray alloc] initwithobjects:@"otherdata", nil];          [self.collectionview performbatchupdates:^{             int resultssize = [self.artistarray count]; //data previous array of data             [self.artistarray addobjectsfromarray:newdata];             nsmutablearray *arraywithindexpaths = [nsmutablearray array];              (int = resultssize; < resultssize + newdata.count; i++) {                 [arraywithindexpaths addobject:[nsindexpath indexpathforrow:i                                                                   insection:0]];             }             [self.collectionview insertitemsatindexpaths:arraywithindexpaths];         } completion:nil];      } 

if guys have questions let me know , try best answer you.

i suppose

[responseobject objectforkey:@"data"]; 

returns immutable object. add mutablecopy

[[responseobject objectforkey:@"data"] mutablecopy]; 

or can clear array , add new objects instead of strong assignment

[self.artistarray removeallobjects]; [self.artistarray addobjectsfromarray:[responseobject objectforkey:@"data"]]; 

upd

if try add new cells should synchronize datasource , collection cells. try this

self.paginationarray = [responseobject objectforkey:@"pagination"]; if(self.firstrequest){     [self.artistarray removeallobjects];     [self.collectionview reloaddata];     self.firstrequest = false; } else {     nsarray *newdata = [[nsarray alloc] initwithobjects:@"otherdata", nil];      [self.collectionview performbatchupdates:^{         int resultssize = [self.artistarray count]; //data previous array of data         [self.artistarray addobjectsfromarray:newdata];         [self.artistarray addobjectsfromarray:[responseobject objectforkey:@"data"]];         nsmutablearray *arraywithindexpaths = [nsmutablearray array];         (int = resultssize; < resultssize + newdata.count; i++) {             [arraywithindexpaths addobject:[nsindexpath indexpathforrow:i                                                               insection:0]];         }         [self.collectionview insertitemsatindexpaths:arraywithindexpaths];     } completion:nil]; } 

Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -