2015-07-21 46 views
0

我想找到一種方法將細胞添加到集合中,並且它一直給我「發送給不可變對象的變異方法」錯誤。我不知道爲什麼。下面發佈的是我正在使用的代碼。將細胞添加到現有的UICollectionView(將變異方法發送到不可變對象)

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 is the previous array of data 
      [self.artistArray addObjectsFromArray:newData]; 
      NSMutableArray *arrayWithIndexPaths = [NSMutableArray array]; 

      for (int i = resultsSize; i < resultsSize + newData.count; i++) { 
       [arrayWithIndexPaths addObject:[NSIndexPath indexPathForRow:i 
                    inSection:0]]; 
      } 
      [self.collectionView insertItemsAtIndexPaths:arrayWithIndexPaths]; 
     } completion:nil]; 

    } 

如果你們有任何問題,讓我知道,我會盡我所能來回答你。

+0

請,秀** ** artistArray財產申報串 – Brain89

+0

@ Brain89的delcaration是: 'self.artistArray = [[NSMutableArray裏的alloc]初始化]。'在viewDidLoad – hparmar

回答

2

我想

[responseObject objectForKey:@"data"]; 

回報不可變對象。只需添加mutableCopy

[[responseObject objectForKey:@"data"] mutableCopy]; 

也可以清除陣列,並添加的,而不是強分配新對象

[self.artistArray removeAllObjects]; 
[self.artistArray addObjectsFromArray:[responseObject objectForKey:@"data"]]; 

UPD

如果您嘗試添加新的細胞,你應該同步你的數據源和收集細胞。因此,嘗試這種

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 is the previous array of data 
     [self.artistArray addObjectsFromArray:newData]; 
     [self.artistArray addObjectsFromArray:[responseObject objectForKey:@"data"]]; 
     NSMutableArray *arrayWithIndexPaths = [NSMutableArray array]; 
     for (int i = resultsSize; i < resultsSize + newData.count; i++) { 
      [arrayWithIndexPaths addObject:[NSIndexPath indexPathForRow:i 
                   inSection:0]]; 
     } 
     [self.collectionView insertItemsAtIndexPaths:arrayWithIndexPaths]; 
    } completion:nil]; 
} 
+0

這部分工作表示感謝。我現在正在得到一些其他錯誤。 performBatchUpdates的EXC_BAD_ACCESS。 但你的答案糾正了第一個問題! – hparmar

+0

@hparmar我根據你的任務更新了答案 – Brain89

相關問題