我想在我的应用中使用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
加载此视图控制器时,我得到
由于未捕获的异常而终止应用 &#39; NSInternalInconsistencyException&#39;,原因: &#39; - [UICollectionViewController loadView]加载了 &#34;吕岛-Nd基2MQ - 视图 - KHW-KE-本&#34; nib但没有获得UICollectionView。&#39;
在主故事板文件中,我有一个UICollectionViewController,其类和故事板ID设置为&#39; AlbumCollectionViewController&#39;。
如果我需要设置委托和数据源连接,我该如何在代码中执行此操作?或者它只能在故事板文件上完成。
此外,如果这似乎不是造成这种情况的原因,我还能做些什么来解决这个问题?
谢谢!
克里斯
答案 0 :(得分:1)
您的CollectionViewController类中不需要IBoutlet cView。已经有一个名为.collectionview的属性由框架实现。
摆脱它,并确保您的集合视图已正确链接到故事板中的视图控制器。
答案 1 :(得分:1)
看看你的故事板。然后检查Settings
。我相信你忘了设置cell id
。
答案 2 :(得分:0)
确保将委托和数据源分配给集合视图,并且还需要将流程布局分配给集合视图。您可以访问像self.collectionView
这样的集合视图,因为类本身是UICollectionViewController
[self.collectionView setDelegate:self];
[self.collectionView setDataSource:self];
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
[self.collectionView setCollectionViewLayout:flowLayout];
如果它还没有奏效, 尝试删除.xib文件中的集合视图的出口并再次将其提供给文件所有者,同样对数据源和委托执行相同操作,并确保文件所有者的类与集合视图控制器类相同
希望这有帮助
答案 3 :(得分:0)
它看起来好像是在混合使用Storyboard和Nib并使用initWithNibName:当你应该使用instantiateViewControllerWithIdentifier:
您获得的错误来自加载nib文件。但是你说你在故事板中创建了视图。如果您使用的是故事板,并且应用程序无法找到CollectionView,则会显示错误
NSInvalidArgumentException&#39;,原因:&#39; Storyboard()不包含带标识符的视图控制器 &#39; AlbumCollectionViewController&#39;
要修复它,如果您使用的是带有AlbumCollectionViewController类型的UICollectionViewController的.storyboard文件,请使用
进行实例化UIStoryboard* sb = [UIStoryboard storyboardWithName:@"NameOfYourStoryboard" bundle:nil];
AlbumCollectionViewController* collectionVC = [sb instantiateViewControllerWithIdentifier:@"AlbumCollectionViewController"];
如果您实际上正在使用.nib文件来保存您的UICollectionViewController,请使用
进行实例化AlbumCollectionViewController* collectionVC = [[AlbumCollectionViewController alloc] initWithNibName:@"NameOfYourNib" bundle:nil];