0

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!

David
  • 319
  • 2
  • 5
  • 18

1 Answers1

1

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.

Anil Varghese
  • 42,757
  • 9
  • 93
  • 110
  • ahhh ok how would I fix this, I have edited my question to include detail view if it helps? Thank you for all of your help it's massively appreciated! – David May 17 '13 at 11:32
  • Yea just the one section, how would I add a title into this as well? – David May 17 '13 at 11:48
  • title on the top of nav?? just self.title = @"Title"; – Anil Varghese May 17 '13 at 11:49
  • - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([segue.identifier isEqualToString:@"detailView"]) { DetailViewController *destViewController = segue.destinationViewController; NSIndexPath *indexPath = [self.collectionView]; destViewController.recipeImageName = [marbleImages objectAtIndex:indexPath.row]; [self.collectionView deselectItemAtIndexPath:indexPath animated:NO]; } – David May 17 '13 at 12:07
  • is that correct? Sorry very new to this! It comes up with an error at NSIndexPath *indexPath = [self.collectionView]; saying expected identifier? – David May 17 '13 at 12:07
  • NSIndexPath *indexPath = [self.collectionView indexPathForSelectedItem]; u missed :) – Anil Varghese May 17 '13 at 12:09
  • Dont feel bad.. u can ask anything here:) – Anil Varghese May 17 '13 at 12:13
  • Thanks for this I know I'm a pain and it's probably straight forward but I've frazzled my brain trying this so nothing is going in! After changing that part it is still crashing. Says No viable @interface for 'UICollectionView' declares the selector 'indexPathForSelectedItem'. I tried changing it to IndexPathsForSelectedItems and this then loads but then crashes when a cell is clicked – David May 17 '13 at 12:27
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/30125/discussion-between-anil-and-david) – Anil Varghese May 17 '13 at 12:31
  • let me know when you are here:) – Anil Varghese May 22 '13 at 05:56