2014-06-30 37 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

相關問題