我想在选择一行后填充数组。现在我想使用函数填充数组。情况就是这样。我有一个显示相册标题的表格视图。选择一行时,将使用 FGallery 库显示所有图片。这是我用于填充数组的函数。
- (NSMutableArray *)getAllPicturesOfAlbumId: (int)AlbumId
{
NSLog(@"tot hier");
_picturesForAlbum = [[NSMutableArray alloc]init];
NSArray *results = [[NSArray alloc]init];
//picture_Url = @"";
NSLog(@"tot hier");
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"whichAlbum.album_id == %d", AlbumId];
[request setEntity:[NSEntityDescription entityForName:@"Picture" inManagedObjectContext:self.genkDatabase.managedObjectContext]];
[request setPredicate:predicate];
NSError *error = nil;
NSLog(@"tot hier");
results = [self.genkDatabase.managedObjectContext executeFetchRequest:request error:&error];
if (results == nil) {
// handle errors
NSLog(@"geen resultaten");
} else if (results.count == 0) {
// nothing found
NSLog(@"0 resultaten");
} else {
for(int i = 0; i < results.count ; i++){
// NSLog(@"%@",[results valueForKey:@"url"]);
[_picturesForAlbum addObject: [results valueForKey:@"url"]];
}
}
return _picturesForAlbum;
}
这是我的 FGallery 方法。
- (int)numberOfPhotosForPhotoGallery:(FGalleryViewController *)gallery
{
return [networkImages count];
}
- (FGalleryPhotoSourceType)photoGallery:(FGalleryViewController *)gallery sourceTypeForPhotoAtIndex:(NSUInteger)index
{
return FGalleryPhotoSourceTypeNetwork;
}
- (NSString*)photoGallery:(FGalleryViewController *)gallery urlForPhotoSize:(FGalleryPhotoSize)size atIndex:(NSUInteger)index {
return [networkImages objectAtIndex:index];
}
这就是我在我的didSelectRowAtIndexPath中所做的
。networkImages = [[NSArray alloc] initWithArray:[self getAllPicturesOfAlbumId:indexPath.row]];
networkGallery = [[FGalleryViewController alloc] initWithPhotoSource:self];
[self.navigationController pushViewController:networkGallery animated:YES];
出于某种原因,我的网络图像被多次填充,它从方法"getAllPicturesOfAlbumId"返回因此,我想我得到以下错误。
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI length]: unrecognized selector sent to instance 0xa15d090'
有人知道问题是什么吗?
提前谢谢。
在数组上调用 valueForKey 将返回该键的所有值的数组。因此,在方法getAllPicturesOfAlbumId中,您不应该循环遍历结果,只需返回[results valueForKey:@"URL"](因此甚至不需要创建_picturesForAlbum数组)。