以编程方式添加CollectionView

时间:2014-02-12 08:31:13

标签: ios objective-c collectionview

我正在尝试使用图像将CollectionView添加到我的应用程序中。所以我在xib文件中设置了一个单元格并添加了一个imageview,现在给出了单元格标识符“Cell”,并将它的委托和数据源设置为文件的所有者。

我现在遇到的问题是,只要我使用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) 

这是我的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

请注意,在使用Objective C进行编码时,我是一个完整的菜鸟。我正在尝试研究它。

2 个答案:

答案 0 :(得分:1)

我所要做的就是改变

@interface CollectionViewController : UICollectionViewController

@interface CollectionViewController : UIViewController

现在没有错误,一切都按预期工作。

答案 1 :(得分:0)

您需要将xib单元注册到视图控制器的 - (void)viewDidLoad上的UICollectionView。

[_tableView registerNib:[UINib nibWithNibName:@"nibName" 
                                       bundle:nil] 
 forCellReuseIdentifier:@"cellIdentifer"];