0

When I start the application, the view looks like this. http://i.imgur.com/vHi2zj0.png

The CollectionView basically overflows from the bottom, meaning that part of it can not be seen even if I scroll down.

However, as soon as I rotate the device to left and then back to right (from portrait to landscape and back to portrait) it goes to what I want it to look like. http://i.imgur.com/QdlV7xB.png

Is there a way to refresh the view in viewDidLoad so that it would look like it looks after rotating twice?

CollectionViewController.m #import "CollectionViewController.h"

@interface CollectionViewController () {
    NSArray * listImages;
}

@end

@implementation CollectionViewController

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

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"bg.jpg"]]];
    // Do any additional setup after loading the view.
    listImages = [NSArray arrayWithObjects:@"7513-kirjatkuivumassa.jpg", @"kuppi.jpg", @"kuva1.jpg", @"juna-042.jpg", @"rautio-valamonruusut-helleaamuna-maalaus.jpg", @"pysähtynyt1.jpg", @"Screen-Shot-2013-02-20-at-21.07.38.jpg", @"sateenkaari.jpg", @"Screen-Shot-2013-02-21-at-17.04.22.jpg", @"moninaiset-e1391026376696.jpg", @"Tomperi+Metsä20111.jpg", @"3-shinot.jpg", @"Ulpukat.jpg", @"janne-e1391025808211.jpg", @"martikainen-240x240.jpg", @"takala-240x240.jpg", @"paanukallokaarme1.jpg", @"käsityök-240x240.jpg", @"kuvis-004.jpg", @"Se-on-hieno-2012-tammi-105x28x223.jpg", nil];
}

- (void)viewDidLayoutSubviews
{
    [self.collectionView reloadData];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return [listImages count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];

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

    return cell;
}

CollectionViewController.h

#import <UIKit/UIKit.h>

@interface CollectionViewController : UIViewController

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@end
Wokki
  • 157
  • 2
  • 18

2 Answers2

0

You will need to create a reference to your UICollectionView in your ViewController.h file.

Do the following:

1) Open Assistant Editor. ViewController.h should be automatically selected, if not, select it.

2) Control-drag your `UICollectionView into your .h file.

3) A pop up will ask you to name your view. Type in collectionView.

4) Add the following code to your .m file

- (void)viewDidLayoutSubviews
{
    [self.collectionView reloadData];
}
Pranav
  • 680
  • 6
  • 11
  • I'm beginner at Objective-C. Added that to viewDidLoad, but I get this "Unknown receiver 'collectionView'; did you mean 'UICollectionView'?" Assuming I need to define collectionView somewhere, but how? – Wokki Feb 14 '14 at 10:22
  • Could you post some code? What have you called your `UICollectionView`, and where have you declared it? – Pranav Feb 14 '14 at 10:27
  • Added CollectionViewController.m code. I believe it's collectionView. – Wokki Feb 14 '14 at 10:30
  • Tried following your instructions. No more errors, but the collectionView is not affected. It still does the same. – Wokki Feb 14 '14 at 10:57
  • Try calling it twice. – Pranav Feb 14 '14 at 11:05
  • Still the same. I tried calling it even 10 times, but still nothing. – Wokki Feb 14 '14 at 11:09
  • Tried with the viewDidLayoutSubviews, but still nothing. I updated the post above with the recent m and h files. – Wokki Feb 14 '14 at 11:35
  • I should have probably also mentioned that there are contraints in the UICollectionView. http://i.imgur.com/x262hit.png – Wokki Feb 14 '14 at 11:43
0

try this

[self.collectionView reloadData];
Sport
  • 8,570
  • 6
  • 46
  • 65