2017-02-15 10 views
0

Я хочу сделать простой пример.UICollectionView в UINavgationController

У меня есть rootViewController, который является UINavgationController, и есть в этом UINavgationControllerUIViewController. Теперь я хочу создать UICollectionView в UIViewController, но я получаю сообщение об ошибке.

вот код:

#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 - должен зарегистрировать перо или класс для идентификатора или подключить прототип ячейки в раскадровке»

Пожалуйста, помогите мне понять, что такое Prob лемм есть.

ответ

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

Идентификатор здесь отличается от того, вы используете в -cellForItemAtIndexPath