gpt4 book ai didi

ios - 如何水平和垂直滚动 CollectionView 单元格..我正在使用 9 个单元格 CollectionView

转载 作者:行者123 更新时间:2023-11-29 03:27:18 28 4
gpt4 key购买 nike

我对 Collection View 完全陌生,请帮助我在 Collection View 中垂直和水平滚动单元格:

viewcontroller.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UICollectionViewDataSource,UICollectionViewDelegate>
{

UICollectionView *collectionView;

}
@end



viewcontroller.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];

UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];

collectionView=[[UICollectionView alloc] initWithFrame:self.view.frame
collectionViewLayout:layout];

[collectionView setDataSource:self];

[collectionView setDelegate:self];

[collectionView registerClass:[UICollectionViewCell class]

forCellWithReuseIdentifier:@"cellIdentifier"];

[collectionView setBackgroundColor:[UIColor redColor]];

[self.view addSubview:collectionView];

// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{

return 9;

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

UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];


cell.backgroundColor=[UIColor greenColor];

return cell;
}

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

return CGSizeMake(100, 100);

}
@end

最佳答案

我来晚了一点,但为了以防万一其他人查找到这一点,您可以使用 UICollectionViewFlowLayout 轻松设置 Collection View 滚动方向。

  1. 创建 UICollectionViewFlowLayout 的实例
  2. 将scrollDirection设置为UICollectionViewScrollDirectionHorizo​​ntal
  3. 庆幸。

    -(实例类型)初始化{ UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init]; 布局.itemSize = CGSizeMake(106, 106); 布局.minimumInteritemSpacing = 1; 布局.minimumLineSpacing = 1; 布局.scrollDirection = UICollectionViewScrollDirectionHorizo​​ntal; self= [ super initWithCollectionViewLayout:layout]; 返回 self ;

关于ios - 如何水平和垂直滚动 CollectionView 单元格..我正在使用 9 个单元格 CollectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20300303/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com