我想在我的应用程序中使用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
当加载这个视图控制器时,我得到
由于未捕获的异常而终止应用程序
“NSInternalInconsistencyException”,原因:
'-[UICollectionViewController loadView]已加载
“Lyo-Nd-2MQ-view-KhW-kE-Ben”笔尖,但没有得到UICollectionView。
在主故事板文件上,我有一个UICollectionViewController,其类和故事板ID设置为“AlbumCollectionViewController”。
如果需要设置委托和数据源连接,该如何在代码中进行?或者只能在情节提要文件上完成。
另外,如果这似乎不是造成此问题的原因,那么我还可以采取其他措施来解决此问题?
谢谢!
克里斯
最佳答案
您在CollectionViewController类中不需要IBoutlet cView。框架已经实现了名为.collectionview的属性。
摆脱这种情况,并确保您的收藏夹视图已正确链接到情节提要中的视图控制器。
关于ios - UICollectionViewController:[UICollectionViewController loadView]加载了“标识符” Nib ,但未获取UICollectionView,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28431326/