我在ViewController中以编程方式实现了Collection View,并将其与Storyboard连接,但是滚动不起作用,一半的单元格没有出现,因为它们褪色到右边:
- (void)viewDidLoad {
[super viewDidLoad];
[self.collectionView registerClass:[FotoCell class]
forCellWithReuseIdentifier:@"cell"];
UICollectionViewFlowLayout *myLayout = [[[UICollectionViewFlowLayout alloc]init]autorelease];
[myLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
[self.collectionView setCollectionViewLayout:myLayout];
}
你知道为什么吗?
答案 0 :(得分:1)
您需要删除viewDidLoad中的registerClass行,并在UICollectionViewDelegate的Datasource方法中设置重用标识符,如下所示 -
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath: (NSIndexPath *)indexPath
{
FotoCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
....
return cell;
}