我是新來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,你可以在這裏看到:
有沒有人有任何想法,爲什麼我得到這個錯誤?一切都歡迎!
編輯:
- (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];
}
有你把這樣的代碼: - (無效)viewDidLoad中{ [超級viewDidLoad中] [self.collectionView registerClass:[FotoCell class] forCellWithReuseIdentifier:@「cell」]; UICollectionViewFlowLayout * myLayout = [[[[UICollectionViewFlowLayout alloc] init] autorelease]; [myLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal]; [self.collectionView setCollectionViewLayout:myLayout]; } – 2013-03-07 12:22:21
錯誤是在這裏: - CollectionCell * aCell =(CollectionCell *)[的CollectionView dequeueReusableCellWithReuseIdentifier:@ 「了myCell」 forIndexPath:indexPath]; – 2013-03-07 12:36:11
UICollectionViewCell * aCell = [collectionView dequeueReusableCellWithReuseIdentifier:@「Cell」forIndexPath:indexPath]; – 2013-03-07 12:37:00