objective-c 如何在屏幕上随时只显示集合视图的一个单元格

How to display only one cell of the collection view on the screen anytime in objective-c

我有一个 collectionview 自定义 collectionviewcell。我想制作一个画廊,任何时候屏幕上只显示一个图像。我必须做什么? 这是我的代码:

[myCollectionView registerClass:[myCustomCollectionViewCell class] forCellWithReuseIdentifier:"myIdentifier"];

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    myCustomCollectionViewCell *cell = (myCustomCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"myIdentifier" forIndexPath:indexPath];
    cell.layer.masksToBounds=NO;
    cell.imageView.image = [UIImage imageNamed:@"image"];
    return cell;

}

首先,像下面这样注册您的集合视图class

[yourCollectionView registerClass:[yourCustomCollectionViewCell class] forCellWithReuseIdentifier:"yourIdentifier"]; 

二、实现集合视图数据源方法

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 1;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    yourCustomCollectionViewCell *cell = (yourCustomCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"yourIdentifier" forIndexPath:indexPath];
    cell.layer.masksToBounds=NO;
    cell.imageView.image = [UIImage imageNamed:@"yourImageName"];
    return cell;

}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

    // update size accordingly
    return CGSizeMake(100,100);
}