aaip
aaip

Reputation: 31

Collection views - receiving images from different folders - how?

I'm creating a tabbar app with two collection views. I successfully have 2 tab bars working with a collection view but they're both receiving data from the same images folder - thumbs & full. So, the images are currently the same on both tabs.

I was hoping to find out what the best way would be to get different images appearing from different folders, so they're not the same?

If you need any further info let me know.

NSString *kDetailedViewControllerID = @"DetailView";   
NSString *kCellID = @"cellID";                          




@implementation ViewController

- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section;
{
    return 29;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath;
{

    Cell *cell = [cv dequeueReusableCellWithReuseIdentifier:kCellID forIndexPath:indexPath];



    NSString *imageToLoad = [NSString stringWithFormat:@"%d.JPG", indexPath.row];
    cell.image.image = [UIImage imageNamed:imageToLoad];

    return cell;
}



- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"showDetail"])
    {
        NSIndexPath *selectedIndexPath = [[self.collectionView indexPathsForSelectedItems] objectAtIndex:0];

        // load the image, to prevent it from being cached we use 'initWithContentsOfFile'
        NSString *imageNameToLoad = [NSString stringWithFormat:@"%d_full", selectedIndexPath.row];
        NSString *pathToImage = [[NSBundle mainBundle] pathForResource:imageNameToLoad ofType:@"JPG"];
        UIImage *image = [[UIImage alloc] initWithContentsOfFile:pathToImage];

        DetailViewController *detailViewController = [segue destinationViewController];
        detailViewController.image = image;
    }
}

@end

Upvotes: 0

Views: 102

Answers (1)

Wain
Wain

Reputation: 119031

When using imageWithName or [[NSBundle mainBundle] pathForResource:imageNameToLoad ofType:@"JPG"] you would be expected to control access to images of different size not with different folders but with different file names. If you want to use different folders then you usually wouldn't use NSBundle to do the loading.

From your comment, you don't even necessarily need to use a different class or duplicate. Think about reuse. If the logic of your controller is the same but the image size / location is different then you can add a couple of properties to the class to set these details and then that one class can be instantiated and configured to work in both situations.

Upvotes: 1

Related Questions