2017-02-15 21 views
0

我想做一個簡單的例子。UINavgationController中的UICollectionView

我有一個rootViewController這是一個UINavgationController,並且在那UINavgationController有一個UIViewController。現在我想在UIViewController中創建UICollectionView,但是出現錯誤。

這裏是代碼:

#import "AppDelegate.h" 
#import "XJViewController.h" 
@interface AppDelegate() 

@end 

@implementation AppDelegate 


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    // Override point for customization after application launch. 

    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds]; 

    XJViewController *xjv = [[XJViewController alloc]init]; 

    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:xjv]; 

    self.window.rootViewController = nav; 

    [self.window makeKeyAndVisible]; 

    return YES; 
} 

#import <UIKit/UIKit.h> 

@interface XJViewController : UIViewController<UICollectionViewDelegate,UICollectionViewDataSource> 

@end 

#import "XJViewController.h" 

@interface XJViewController() 

@end 

@implementation XJViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 
    self.view.backgroundColor = [UIColor whiteColor]; 

    self.title = @"XJ"; 

    self.navigationController.navigationBar.barTintColor = [UIColor redColor]; 

    NSDictionary *dicColor = @{NSForegroundColorAttributeName : [UIColor whiteColor]}; 

    self.navigationController.navigationBar.titleTextAttributes = dicColor; 

    UICollectionViewFlowLayout *fLayout = [[UICollectionViewFlowLayout alloc]init]; 

    UICollectionView *cv = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:fLayout]; 


    [cv registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"myeCell"]; 

    cv.dataSource = self; 
    cv.delegate = self; 

    [self.view addSubview:cv]; 

} 

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section 
{ 
    return 4; 
} 

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"myCell" forIndexPath:indexPath]; 

    cell.backgroundColor = [UIColor redColor]; 

    return cell; 
} 

***終止應用程序由於未捕獲的異常 'NSInternalInconsistencyException',理由是:「不能出列類型的視圖 :UICollectionElementKindCell與標識了myCell - 必須 註冊一個筆尖或類的標識符或連接原型 單元格中的故事板'

請幫我弄清楚什麼是概率lem是。

回答

0
[cv registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"myeCell"]; 

這裏的標識是從一個不同的你正在使用-cellForItemAtIndexPath

相關問題