0

我在應用程序中使用UICollectionView,其中集合視圖的單元格基於來自服務器的數據。UICollectionViewCell對NSMutableArray的訪問錯誤

當應用程序加載我從服務器下載的數據,並將其轉換成被存儲在該dataArray中的一個NSMutableArray

@property (nonatomic, retain) NSMutableArray *dataArray; 

計數個體NSObjects然後被用於確定在細胞的數量收集視圖。

當我嘗試在cellForItemAtIndexPath集合視圖的委託方法訪問此dataArray中的問題就出現了:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UICollectionViewCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"ReminderCard" forIndexPath:indexPath]; 

    GMReminderCard *reminderCard = (GMReminderCard *)cell; 
    GMReminder *rem = [self.dataArray objectAtIndex:[indexPath row]]; 

    [reminderCard initialiseReminder:rem]; 

    cell.layer.cornerRadius  = 5; 
    cell.layer.masksToBounds = YES; 
    return cell; 
} 


- (void)initialiseReminder:(GMReminder *)rem 
{ 
    if(!loan)return; 

    _thisReminder = rem; 


    NSURL *imageURL = [NSURL URLWithString:_thisReminder.image relativeToURL:[NSURL URLWithString:@"http://host.cloudfront.net/"]]; 

    [self.image setImageWithURL:[imageURL absoluteURL] 
      placeholderImage:[UIImage imageNamed:@"973-user.png"]]; 

    [_title setText:[_thisReminder title]]; 
    [_description setText:[_thisReminder description]]; 
} 

這是我從服務器獲取數據的代碼中,NSMutableArray中返回包含GMReminder對象:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self becomeFirstResponder]; 

    dataArray = [[NSMutableArray alloc] init]; 

    [GMReminders getCurrentReminders:^(NSMutableArray *rems, NSError *err){ 
     if (!err) { 
      dataArray = [[NSMutableArray alloc] initWithArray:rems]; 
      [collectionView reloadData]; 
     } 
    }]; 

    [self.view setBackgroundColor:[UIColor blackColor]]; 

    [self buildBackgroundSlider]; 
    [self buildLabel]; 
    [self buildCollectionView]; 
} 

最初,單元格按照預期方式加載,但是當我嘗試滾動集合視圖時,第一次嘗試使用GMReminder變量時出現錯誤的訪問錯誤。

任何幫助,將不勝感激

+0

'initialiseReminder:'做什麼? –

+0

我已經添加了initialiseReminder方法 –

回答

0

在viewDidLoad方法做出的CollectionView編程: -

UICollectionView *collection_view=[[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, 320, [UIScreen mainScreen].bounds.size.height) collectionViewLayout:layout]; 
    [collection_view setBackgroundColor:[UIColor colorWithRed:0.810746 green:0.764681 blue:0.764681 alpha:0.8]]; 
    collection_view.delegate = self; 
    collection_view.dataSource = self; 
    [collection_view registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellIdentifier"]; 
    [self.view addSubview:collection_view]; 

定義委託UicollectionView的細胞: -

- (UICollectionViewCell *) collectionView:(UICollectionView *) collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *[email protected]"cellIdentifier"; 

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath]; 

    if (!cell) 
    { 
     cell = [[UICollectionViewCell alloc] initWithFrame:CGRectMake(1,0,cell.frame.size.width, cell.frame.size.height)]; 
    } 
    return cell; 
} 
+0

這並不是真正的幫助,因爲構建單元格不是問題,它使用正在崩潰應用程序的_dataArray初始化單元格內的數據 –

+0

plz在viewdidload中嘗試此操作self.dataArray = [ [NSMutableArray alloc] init]; – SilentStalker

+0

我已經在我的代碼中初始化了這個數組。在原始問題中添加從服務器獲取數據的方法 –

0

替換此

dataArray = [[NSMutableArray alloc] initWithArray:rems]; 

dataArray = [[NSMutableArray alloc] initWithArray:[rems copy]]; 
+0

使用副本時仍然會出現相同的錯誤 –