2013-03-07 103 views
3

我是新來UICollectionView,我下面的教程,我在YouTube上找到被初始化,但我被困在一個錯誤我想不出。控制檯錯誤:UICollectionView必須以非空佈局參數

當我運行這段代碼的應用程序:

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { 

     return 1; 

    } 

    -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { 

     return [self.array count]; 

    } 

    -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 

     CollectionCell *aCell = (CollectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"myCell" forIndexPath:indexPath]; 

     aCell.title.text = self.array[indexPath.row]; 

     return aCell; 

    } 

    - (void)viewDidLoad 
    { 
     [super viewDidLoad]; 

     self.array = @[@"First", @"Second", @"Thirth", @"Fourth"]; 

    } 

而在.H:

@property (strong, nonatomic) NSArray *array; 

在我收到以下錯誤控制檯:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'UICollectionView must be initialized with a non-nil layout parameter' 

林不使用故事板,並且自定義了CollectionView,你可以在這裏看到:

image

有沒有人有任何想法,爲什麼我得到這個錯誤?一切都歡迎!

編輯:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.array = @[@"First", @"Second", @"Thirth", @"Fourth"]; 

    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"myCell"]; 

    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init]; 
    [flow setItemSize:CGSizeMake(60, 60)]; 
    [flow setScrollDirection:UICollectionViewScrollDirectionVertical]; 

    [self.collectionView setCollectionViewLayout:flow]; 

} 

回答

5

它是一個錯誤而註冊uicollectionviewcell視圖類。要解決把下面的行代碼:

[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"myCell"]; 
+0

有你把這樣的代碼: - (無效)viewDidLoad中{ [超級viewDidLoad中] [self.collectionView registerClass:[FotoCell class] forCellWithReuseIdentifier:@「cell」]; UICollectionViewFlowLayout * myLayout = [[[[UICollectionViewFlowLayout alloc] init] autorelease]; [myLayout setScrollDirection:UICollectionViewScrollDirectionHorizo​​ntal]; [self.collectionView setCollectionViewLayout:myLayout]; } – 2013-03-07 12:22:21

+0

錯誤是在這裏: - CollectionCell * aCell =(CollectionCell *)[的CollectionView dequeueReusableCellWithReuseIdentifier:@ 「了myCell」 forIndexPath:indexPath]; – 2013-03-07 12:36:11

+0

UICollectionViewCell * aCell = [collectionView dequeueReusableCellWithReuseIdentifier:@「Cell」forIndexPath:indexPath]; – 2013-03-07 12:37:00

2

註冊收藏細胞在viewDidLoad方法::

[self.collView registerClass:[mineCell class] forCellWithReuseIdentifier:@"cvCell"]; 

試試這個上面的行之後:

UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init]; 
[flow setItemSize:CGSizeMake(60, 60)]; 
[flow setScrollDirection:UICollectionViewScrollDirectionVertical]; 

[self.collView setCollectionViewLayout:flow]; 

這裏,mineCell是我的自定義collectionViewCell,或者您可以直接使用[UICollectionViewCell class]

謝謝。

+1

將代碼放入viewDidLoad方法後,錯誤不會消失?它仍然給我同樣的錯誤。 – 2013-03-07 12:18:57

+0

試試這個我的答案 – 2013-03-07 12:19:40

相關問題