I want to populate an array after I selected a row. Now I want to populate an array using a function. This is what the situation is. I have a tableview which displays albums titles. When you select a row you show all pictures using the FGallery library. Here is my function for populating my array.
- (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;
}
Here are my FGallery methods.
- (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];
}
And this is what I do in my didSelectRowAtIndexPath
networkImages = [[NSArray alloc] initWithArray:[self getAllPicturesOfAlbumId:indexPath.row]];
networkGallery = [[FGalleryViewController alloc] initWithPhotoSource:self];
[self.navigationController pushViewController:networkGallery animated:YES];
Put for some reason or another my networkImages is populated several times with the array it gets back from the method “getAllPicturesOfAlbumId”
Because of that I think I get the following error.
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI length]: unrecognized selector sent to instance 0xa15d090'
Anybody has an idea what the problem is?
Thanks in advance.
Calling valueForKey on an array returns an array of all the value for that key. So, in the method getAllPicturesOfAlbumId, you shouldn’t loop through results, just return [results valueForKey:@”URL”] (so no need to even create the _picturesForAlbum array).