使用数据填充NSArray

时间:2012-10-15 21:45:42

标签: objective-c ios nsmutablearray nsarray image-gallery

我想在选择一行后填充数组。现在我想使用函数填充数组。情况就是这样。我有一个tableview显示专辑标题。选择行时,使用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];

出于某种原因,我的networkImages被填充了几次,它从方法“getAllPicturesOfAlbumId”返回 因此,我认为我得到以下错误。

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI length]: unrecognized selector sent to instance 0xa15d090'

任何人都知道问题是什么?

提前致谢。

1 个答案:

答案 0 :(得分:0)

在数组上调用valueForKey会返回该键的所有值的数组。所以,在方法getAllPicturesOfAlbumId中,你不应该遍历结果,只返回[results valueForKey:@“URL”](所以不需要甚至创建_picturesForAlbum数组)。