草庐IT

ios - UICollectionViewController : [UICollectionViewController loadView] loaded the "identifier" nib but didn't get a UICollectionView

coder 2024-01-19 原文

我想在我的应用中使用 UICollectionViewController 来显示照片。

我从 UICollectionViewController 派生了一个类:

#import <UIKit/UIKit.h>

@interface AlbumCollectionViewController : UICollectionViewController <UICollectionViewDataSource, UICollectionViewDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView * cview;


@end

实现是:

#import "AlbumCollectionViewController.h"

@interface AlbumCollectionViewController ()

//@property (weak, nonatomic) IBOutlet UICollectionView * cview;

@end

@implementation AlbumCollectionViewController

static NSString * const reuseIdentifier = @"Cell";

- (void)viewDidLoad {
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = NO;

    // Register cell classes
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];

    // Do any additional setup after loading the view.
}

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

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
#warning Incomplete method implementation -- Return the number of sections
    //return 0;
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
#warning Incomplete method implementation -- Return the number of items in the section
    NSUInteger i = 1;
    return i;

}

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

    // Configure the cell

    return cell;
}

#pragma mark <UICollectionViewDelegate>

/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/

/*
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/

/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
    return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
    return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {

}
*/

@end

加载这个 View Controller 时,我得到

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UICollectionViewController loadView] loaded the "Lyo-Nd-2MQ-view-KhW-kE-Ben" nib but didn't get a UICollectionView.'

在主 Storyboard文件中,我有一个 UICollectionViewController,其类和 Storyboard ID 设置为“AlbumCollectionViewController”。

如果我需要设置委托(delegate)和数据源连接,我该如何在代码中执行此操作?或者只能在 Storyboard文件上完成。

此外,如果这似乎不是导致此问题的原因,我还能做些什么来解决这个问题?

谢谢!

克里斯

最佳答案

您的 CollectionViewController 类中不需要 IBoutlet cView。框架已经实现了名为 .collectionview 的属性。

摆脱它,并确保您的 Collection View 正确链接到 Storyboard 中的 View Controller 。

关于ios - UICollectionViewController : [UICollectionViewController loadView] loaded the "identifier" nib but didn't get a UICollectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28431326/

有关ios - UICollectionViewController : [UICollectionViewController loadView] loaded the "identifier" nib but didn't get a UICollectionView的更多相关文章

随机推荐