2017-02-15 2 views
0

Ich möchte ein einfaches Beispiel machen.UICollectionView in UINavgationController

Ich habe ein rootViewController, dass ein UINavgationController ist, und es gibt eine UIViewController in diesen UINavgationController. Jetzt möchte ich UICollectionView in der UIViewController erstellen, aber ich bekomme einen Fehler.

hier ist der Code:

#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; 
} 

*** App beenden aufgrund nicht abgefangene Ausnahme 'NSInternalInconsistencyException' Grund: ‚keine Aussicht von Art dequeue könnte: UICollectionElementKindCell mit Kennung myCell - muss registrieren eine Spitze oder eine Klasse für den Identifier oder einen Prototyp Zelle in einem Storyboard‘verbinden

Bitte helfen Sie mir herauszufinden, was das prob lem ist.

Antwort

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

Die Kennung hier unterscheidet sich von dem verwenden Sie -cellForItemAtIndexPath

in