我在應用程序中使用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變量時出現錯誤的訪問錯誤。
任何幫助,將不勝感激
'initialiseReminder:'做什麼? –
我已經添加了initialiseReminder方法 –