0

내 응용 프로그램에서 UICollectionView를 연결하고로드 할 때마다 검은 색 화면이 표시됩니다.UICollectionView가 프로그래밍 방식으로로드되지 않습니다.

발신자 코드 :

UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init]; 
    [flowLayout setItemSize:CGSizeMake(200, 200)]; 
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal]; 
    MyAppDashBoardHomeViewController *aDashboardViewController = [[MyAppDashBoardHomeViewController alloc] initWithCollectionViewLayout:flowLayout]; 
    [self presentViewController:aDashboardViewController animated:YES completion:nil]; 

UICollectionController 하위 클래스

#import <UIKit/UIKit.h> 

@interface MyAppDashBoardHomeViewController : UICollectionViewController <UICollectionViewDelegate, UICollectionViewDataSource> 

@end 


#import "MyAppDashBoardHomeViewController.h" 
#import "MyAppDashBoardCollectionViewCell.h" 
#import "MyAppCustomNavigationBar.h" 

@interface MyAppDashBoardHomeViewController() 

@property (nonatomic, strong) NSMutableArray *applications; 
@property (nonatomic) UIView *containerView; 
@property (nonatomic, strong) MyAppCustomNavigationBar *customNavBar; 

@end 

@implementation MyAppDashBoardHomeViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view 

    [self.collectionView registerClass:[MyAppDashBoardCollectionViewCell class] forCellWithReuseIdentifier:@"MyAppDashBoardCell"]; 

    self.containerView = [[UIView alloc] initWithFrame:CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y, self.view.bounds.size.width, self.view.bounds.size.height)]; 
    [self.containerView setBackgroundColor:[UIColor blackColor]]; 
    [self.containerView setBackgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.5]]; 
    [self.view addSubview:self.containerView]; 

    // Adding custom navigation bar 
    self.customNavBar = [[MyAppCustomNavigationBar alloc] initWithTitle:kMyAppNewTestTitle detailedTitle:nil informationBarData:nil buttonType:MyAppModalScreen andDelegate:self]; 
    [self.containerView addSubview:self.customNavBar]; 


    self.applications = [[NSMutableArray alloc] initWithObjects:@"Test 1", @"Test 2", @"Test 3", @"Test 4", nil]; 
} 



- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 


- (NSInteger)collectionView:(UICollectionView *)iCollectionView numberOfItemsInSection:(NSInteger)iSection { 
    return self.applications.count; 
} 

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath: 
- (UICollectionViewCell *)collectionView:(UICollectionView *)iCollectionView cellForItemAtIndexPath:(NSIndexPath *)iIndexPath { 
    static NSString *cellIdentifier = @"MyAppDashBoardCell"; 

    MyAppDashBoardCollectionViewCell *aCell = (MyAppDashBoardCollectionViewCell *)[iCollectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:iIndexPath]; 

    if (!aCell) { 
     aCell = [[MyAppDashBoardCollectionViewCell alloc] init]; 
    } 

    aCell.appName = self.applications[iIndexPath.row]; 

    return aCell; 
} 


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)iCollectionView { 
    return 1; 
} 


- (void)backButtonAction:(id)sender { 
    [self dismissViewControllerAnimated:YES completion:nil]; 
} 

UICollectionViewCell 하위 클래스

#import <UIKit/UIKit.h> 

@interface MyAppDashBoardCollectionViewCell : UICollectionViewCell 

@property (nonatomic, strong) NSString *appName; 

@end 



#import "MyAppDashBoardCollectionViewCell.h" 

@interface MyAppDashBoardCollectionViewCell() 

@property (nonatomic, strong) UILabel *appNameLabel; 

@end 

@implementation MyAppDashBoardCollectionViewCell 

- (id)init { 
    self = [super init]; 

    if (self) { 
     self.appNameLabel = [[UILabel alloc] initWithFrame:CGRectZero]; 
     self.appNameLabel.textAlignment = NSTextAlignmentCenter; 
     self.appNameLabel.textColor = [UIColor whiteColor]; 
     self.appNameLabel.font = [UIFont systemFontOfSize:17.0]; 

     self.backgroundColor = [UIColor redColor]; 
     [self.viewForBaselineLayout addSubview:self.appNameLabel]; 
    } 

    return self; 
} 

- (void)layoutSubviews { 
    [super layoutSubviews]; 

    CGSize aTextSize = [self.appName sizeWithFont:[UIFont systemFontOfSize:17.0]]; 
    CGRect appNameFrame = CGRectMake((self.frame.size.width - aTextSize.width)/2, (self.frame.size.height - aTextSize.height)/2, aTextSize.width, aTextSize.height); 
    self.appNameLabel.frame = appNameFrame; 
} 


@end 
,691 아래는 제가 작성한 코드는
+0

아직, 클래스 또는 펜촉을 등록하거나 스토리 보드에 셀을 만들 때 아무런 셀도 확인하지 않아도됩니다. dequeue 메서드를 사용하면 셀을 반환 할 수 있습니다 (또는 올바른 식별자를 지정하지 않으면 충돌이 발생합니다). – rdelmar

답변

1

UICollectionViewCell에 대한 init 메서드는 initWithFrame : (UIView에서 상 속됨)이어야합니다. 따라서 사용자의 일반 init 메서드가 호출되지 않는 것이 문제라고 생각합니다.

+0

실제로. 지적 해 주셔서 고마워요. – Abhinav

관련 문제