我正在嘗試創建一個簡單的View:一個需要80%屏幕的tableView,以及底部有4行的collectionView。一切工作正常與我的tableView,但我不能用我的collectionView正確的事情。我總是得到以下異常:創建collectionViewCell時出現異常
Terminating app due to uncaught exception 'NSInternalInconsistencyException',
reason: 'could not dequeue a view of kind: UICollectionElementKindCell with
identifier simpleCollectionCell - must register a nib or a class for the
identifier or connect a prototype cell in a storyboard'.
我一直在尋找我的視圖控制器:有2個爲我的CollectionView闖民宅網點:數據源和委託。所以沒關係。我正在使用「withReuseIdentifier」方法,所以我不必在筆尖中聲明一個標識符。我一直在試圖爲collectionView創建一個屬性,但它沒有改變。
所以,我真的不明白我錯過了什麼?
.H:
#import <UIKit/UIKit.h>
@interface MainViewController : UIViewController <UITableViewDelegate,
UITableViewDataSource, UICollectionViewDelegate, UICollectionViewDataSource>
@end
.M:
#import "MainViewController.h"
@interface MainViewController()
@end
@implementation MainViewController {
NSArray *recipes;
NSArray *images; }
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self; }
- (void)viewDidLoad {
[super viewDidLoad];
recipes = [NSArray arrayWithObjects:@"Label1", @"Label2", @"Label3", @"Label4", @"Label5", @"Label6", @"Label7", @"Label8", @"Label9", @"Label10", @"Label11", @"Label12", @"Label13", @"Label14", @"Label15", nil];
images = [NSArray arrayWithObjects:@"img1", @"img2", @"img3", @"img4", nil];
// Do any additional setup after loading the view. }
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated. }
#pragma mark TableView methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [recipes count]; }
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableCell";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [recipes objectAtIndex:indexPath.row];
return cell;
}
#pragma mark CollectionView methods
- (NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView
numberOfItemsInSection: (NSInteger)section {
return [images count];
}
- (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView
dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
return cell;
}
@end
提前感謝!
檢查,你沒有給收集細胞「細胞」標識。 – Pawan