2014-06-30 39 views
0

我正在尝试创建一个简单的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 

提前感谢!

+0

检查,你没有给收集细胞“细胞”标识。 – Pawan

回答

1

确保在界面构建器中设置了合适的自定义类(如果有)以及集合视图单元的单元标识符。检查此屏幕截图。

enter image description here

这是自定义类和下面的一个是用于小区标识符。

enter image description here

你的情况标识符为 “细胞”。确保你有这些设置。他们可能是这里的问题。此外,您的CollectionViewCell的类名称和您的cellForItemAtIndexPath中的类别应该相同。

希望这会有所帮助。

+0

设置制作它的单元格的标识符。但我不明白的是,我没有为tableView做,它的工作。此外,我是否必须为每个要创建的单元格创建一个单元格? –

+0

不,这不是必要的。你可以使用'numberOfRowsInSection'来动态生成所需数量的单元格。否则,您可以选择使用静态数量的单元格。 –

1

OKYü可以用这个尝试,只是一个简单的方法,

  • 首先创建一个新的文件中user interface选择view和名称NibCell
  • 然后选择设备(选择其中任何一个)。
  • 删除视图中的任何内容。
  • 只需拖放一个新的CollectionViewCell就是这样.. :)在collectinview细胞ü可以添加图像视图(或U希望任何其他对象)
  • 并设置其标签的访问。

例如

viewDidLoad方法

- (void)viewDidLoad 
    { 
    [super viewDidLoad]; 

    //....other code 

    //nib register 
     UINib *cellNib = [UINib nibWithNibName:@"NibCell" bundle:nil]; 
     [_collectionView registerNib:cellNib forCellWithReuseIdentifier:@"Cell"]; 
    } 

并在此方法

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
    { 
     static NSString *cellIdentifier = @"Cell"; //u know the identifier 
     UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath]; //get the cell 
     UILabel *titleLabel = (UILabel *)[cell viewWithTag:100]; //for example i put a label and set it's tag to 100 and i an accessing it 
    [titleLabel setText:cellData]; 
    return cell; 

    } 
1

,你必须注册您的收藏观察室

[自我。dashBoardCollection registerNib:[UINib nibWithNibName:@“DashBoardCell”bundle:nil] forCellWithReuseIdentifier:@“DashBoardCell”];

可能是身份检查你缺少命名类的故事板enter image description here

相关问题