David
David

Reputation: 319

iPhone photo gallery app help needed please

I'm new to collection views and want to know if this is the best way to create them, I also would like some advice on where to go from there to segue to a detail view with paging enabled.

#import "MarbleCollectionViewController.h"
#import "DetailViewController.h"

@interface MarbleCollectionViewController () {
   NSArray *marbleImages;
}

@end

@implementation MarbleCollectionViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
  self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    marbleImages = [NSArray arrayWithObjects:@"arabescato.jpg",
                    @"bianco-carrara.jpg",
                    @"botticino-classico2.jpg",
                    @"Calacatta_Oro.jpg",
                    @"crema-marfil-3.jpg",
                    @"crema-valencia.jpg",
                    @"emperador-dark.jpg",
                    @"jura-beige.jpg",
                    @"nero-marquina.jpg",
                    @"perlato-olympo.jpg",
                    @"rojo-alicante_marbleGRP1.jpg", nil];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return marbleImages.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"Cell";

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

    UIImageView *marbleImageView = (UIImageView *)[cell viewWithTag:100];
    marbleImageView.image = [UIImage imageNamed:[marbleImages objectAtIndex:indexPath.row]];

    return cell;
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([segue.identifier isEqualToString:@"detailView"]) {
        DetailViewController *destViewController = segue.destinationViewController;
        NSIndexPath *indexPath = [self.collectionView ];
        destViewController.recipeImageName = [marbleImages[indexPath.section] objectAtIndex:indexPath.row];
        [self.collectionView deselectItemAtIndexPath:indexPath animated:NO];
    }
}

- (void)didReceiveMemoryWarning

{
   [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.
}

@end

This is my detail view code:

#import "DetailViewController.h"

@interface DetailViewController ()

@end

@implementation DetailViewController

@synthesize recipeImageView;
@synthesize recipeImageName;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.recipeImageView.image = [UIImage imageNamed:self.recipeImageName];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

is another collectionview for the detail view the way to go or a scroll view with paging enabled as I'm not sure how to implement this? I also want to be able to have a title on the top nav bar as well?

Thanks in advance!

Upvotes: 0

Views: 154

Answers (1)

Anil Varghese
Anil Varghese

Reputation: 42977

I can find some coding mistake in your code. It will cause the error. marbleImages is an arry with string objects

marbleImages = [NSArray arrayWithObjects:@"arabescato.jpg",...];

But in prepareForSegue: you are doing like

destViewController.recipeImageName = [marbleImages[indexPath.section] objectAtIndex:indexPath.row]; 

This code expects an array of array objects. The above code can be converted as

 NSString *obj = [marbleImages[indexPath.section]] // It will return string only since you are storing string
[obj objectAtIndex:indexPath.row]// This will cause error since NSString does not have a method objectAtIndex:  

I hope you have only one section(I didnt find noOfSections method). So do like

    destViewController.recipeImageName = [marbleImages objectAtIndex:indexPath.row];

Suggestion for your detail view

I think you are trying to imitate iPhones native photo app. To get the same feel like that you should use scrollview with paging enabled ,even possible with collection view also. One easy implementation is using a collection view.If you adjust the properties of CollectionViewFlowLayout you can achieve that.

Upvotes: 1

Related Questions