0

我想在选择一行后填充一个数组。现在我想使用一个函数填充一个数组。情况就是这样。我有一个显示专辑标题的表格视图。当您选择一行时,您会使用 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'

有人知道问题是什么吗?

提前致谢。

4

1 回答 1

0

对数组调用 valueForKey 会返回一个包含该键的所有值的数组。因此,在 getAllPicturesOfAlbumId 方法中,您不应该循环遍历结果,只需返回 [results valueForKey:@"URL"](因此甚至不需要创建 _picturesForAlbum 数组)。

于 2012-10-15T22:03:10.187 回答