Question

I'm attempting to add a CollectionView to my application with images. So I have setup one cell in the xib file and added an imageview, gave the cell identifier "Cell" for now and set it's delegate and datasource to the file's owner.

The issue I am having right now is that the application crashes whenever I enter the screen with that CollectionView.

2014-02-12 10:26:46.429 CollectionApp[509:a0b] *** Assertion failure in -[CollectionViewController loadView], /SourceCache/UIKit_Sim/UIKit-2903.23/UICollectionViewController.m:166
2014-02-12 10:26:46.510 CollectionApp[509:a0b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UICollectionViewController loadView] loaded the "34q-Sl-gDP-view-cX9-PP-w1h" nib but didn't get a UICollectionView.'
*** First throw call stack:
(
    0   CoreFoundation                      0x0191b5e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x0169e8b6 objc_exception_throw + 44
    2   CoreFoundation                      0x0191b448 +[NSException raise:format:arguments:] + 136
    3   Foundation                          0x0127efee -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116
    4   UIKit                               0x008eaec3 -[UICollectionViewController loadView] + 582
    5   UIKit                               0x003c90ae -[UIViewController loadViewIfRequired] + 78
    6   UIKit                               0x003c95b4 -[UIViewController view] + 35
    7   UIKit                               0x004003ae -[UITabBarController transitionFromViewController:toViewController:transition:shouldSetSelected:] + 407
    8   UIKit                               0x003ffbd2 -[UITabBarController transitionFromViewController:toViewController:] + 63
    9   UIKit                               0x003fbfbb -[UITabBarController _setSelectedViewController:] + 279
    10  UIKit                               0x003fbe9c -[UITabBarController setSelectedViewController:] + 180
    11  UIKit                               0x003fface -[UITabBarController _tabBarItemClicked:] + 312
    12  libobjc.A.dylib                     0x016b0874 -[NSObject performSelector:withObject:withObject:] + 77
    13  UIKit                               0x002b70c2 -[UIApplication sendAction:to:from:forEvent:] + 108
    14  UIKit                               0x002b704e -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
    15  UIKit                               0x00565659 -[UITabBar _sendAction:withEvent:] + 479
    16  libobjc.A.dylib                     0x016b081f -[NSObject performSelector:withObject:] + 70
    17  UIKit                               0x002b70c2 -[UIApplication sendAction:to:from:forEvent:] + 108
    18  UIKit                               0x002b704e -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
    19  UIKit                               0x003af0c1 -[UIControl sendAction:to:forEvent:] + 66
    20  UIKit                               0x003af484 -[UIControl _sendActionsForEvents:withEvent:] + 577
    21  UIKit                               0x003af0f6 -[UIControl sendActionsForControlEvents:] + 48
    22  UIKit                               0x00569f80 -[UITabBar(Static) _buttonUp:] + 123
    23  libobjc.A.dylib                     0x016b0874 -[NSObject performSelector:withObject:withObject:] + 77
    24  UIKit                               0x002b70c2 -[UIApplication sendAction:to:from:forEvent:] + 108
    25  UIKit                               0x002b704e -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
    26  UIKit                               0x003af0c1 -[UIControl sendAction:to:forEvent:] + 66
    27  UIKit                               0x003af484 -[UIControl _sendActionsForEvents:withEvent:] + 577
    28  UIKit                               0x003ae733 -[UIControl touchesEnded:withEvent:] + 641
    29  UIKit                               0x002f451d -[UIWindow _sendTouchesForEvent:] + 852
    30  UIKit                               0x002f5184 -[UIWindow sendEvent:] + 1232
    31  UIKit                               0x002c8e86 -[UIApplication sendEvent:] + 242
    32  UIKit                               0x002b318f _UIApplicationHandleEventQueue + 11421
    33  CoreFoundation                      0x018a483f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
    34  CoreFoundation                      0x018a41cb __CFRunLoopDoSources0 + 235
    35  CoreFoundation                      0x018c129e __CFRunLoopRun + 910
    36  CoreFoundation                      0x018c0ac3 CFRunLoopRunSpecific + 467
    37  CoreFoundation                      0x018c08db CFRunLoopRunInMode + 123
    38  GraphicsServices                    0x0359c9e2 GSEventRunModal + 192
    39  GraphicsServices                    0x0359c809 GSEventRun + 104
    40  UIKit                               0x002b5d3b UIApplicationMain + 1225
    41  CollectionApp                       0x0000ed9d main + 141
    42  libdyld.dylib                       0x01f57725 start + 0
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

Here's my CollectionViewController.h

#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];
    // 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];
}

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

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

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

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

    return cell;
}

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

@end

Please note that I am a complete noob when it comes to coding with Objective C. I'm currently attempting to study it.

Was it helpful?

Solution

All I had to do was to change

@interface CollectionViewController : UICollectionViewController

To

@interface CollectionViewController : UIViewController

Now there's no errors and everything works as intended.

OTHER TIPS

You need register your xib cell to your UICollectionView on -(void)viewDidLoad of your view Controller.

[_tableView registerNib:[UINib nibWithNibName:@"nibName" 
                                       bundle:nil] 
 forCellReuseIdentifier:@"cellIdentifer"];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top