iOS:numberOfSectionsInCollectionView发送到解除分配实例的消息
我有此代码来管理collectionviewiOS:numberOfSectionsInCollectionView发送到解除分配实例的消息,ios,crash,uicollectionview,dispatch-async,Ios,Crash,Uicollectionview,Dispatch Async,我有此代码来管理collectionview - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return [images
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [images count];
}
- (PhotoCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
PhotoCell *cell = [gridPhoto dequeueReusableCellWithReuseIdentifier:@"photocell" forIndexPath:indexPath];
NSMutableDictionary *record = [images objectAtIndex:[indexPath row]];
if ([record valueForKey:@"actualImage"]) {
[cell.image setImage:[record valueForKey:@"actualImage"]];
[cell.activity stopAnimating];
} else {
dispatch_async(imageQueue_, ^{
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[record objectForKey:@"url"]]];
dispatch_async(dispatch_get_main_queue(), ^{
[record setValue:[UIImage imageWithData:imageData] forKey:@"actualImage"];
[collectionView reloadItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]];
});
});
}
return cell;
}
如果imageQueue完成其工作并填充整个collectionview,则工作正常;当我退出视图回叫时,我遇到了一个问题
- (IBAction)back:(id)sender{
[self.navigationController popViewControllerAnimated:YES];
}
在这种情况下,如果collectionview没有完全填充图像,则在执行返回操作时会出现以下错误:
[PhotoGalleryViewController numberOfSectionsInCollectionView:]: message sent to deallocated instance 0x1590aab0
问题出在哪里?当您弹出视图控制器时,下载仍在进行中。因此,当执行异步回调时,您的
[collectionView reloadindexpaths…]
将在不再存在的collectionView
上被调用
您应该检查collectionView!=在回调块的第一行为nil,只需返回return代码>如果为零:
dispatch_async(dispatch_get_main_queue(), ^{
if (collectionView == nil)
return;
[record setValue:[UIImage imageWithData:imageData] forKey:@"actualImage"];
[collectionView reloadItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]];
});
某个人(某个对象)在您的collectionView上有一个强引用,使其在内存中保持活动状态,因此它会不断发送您的委托消息。您能否提供.h文件中的代码以及我在何处设置该控件?在[record setValue:[UIImage imageWithData:imageData]forKey:@“RealityMage”];[collectionView ReloadItemsAndExpaths:[NSArray arrayWithObject:indexPath]]之前的“dispatch\u async(dispatch\u get\u main\u queue(),^”内;它无法识别collectionview==nil,但我解决了在按下back键时将bool值设置为true的问题,并在您告诉我的地方设置了如果此bool为true的条件;它现在可以正常工作,并且每次都可以顺利退出并进入此视图控制器