2017-02-15 1 views
0

간단한 예제를 원합니다.UICollectionView in UINavgationController

나는 UINavgationController이고, 그 중 에 UIViewController이 있습니다. 이제 UIViewControllerUICollectionView을 만들고 싶지만 오류가 발생합니다. 응용 프로그램을 종료

#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'캐치되지 않는 예외 : 여기

코드는 '종류의보기 큐에서 제거 할 수 없습니다 : 식별자 myCell와 UICollectionElementKindCell을 -해야 식별자에 펜촉이나 클래스를 등록하거나 프로토 타입을 연결하십시오.

prob 흠.

답변

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

여기 식별자는 것과 다른 당신은 -cellForItemAtIndexPath

을에서 사용하는