cuốn sách gpt4 ai đã làm

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

In lại Tác giả: Walker 123 更新时间:2023-11-29 03:27:18 28 4
mua khóa gpt4 Nike

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

viewcontroller.h

#import

@interface ViewController : UIViewController
{

UICollectionView *collectionView;

}
@kết thúc



viewcontroller.m

#import "ViewController.h"

@interface ViewController ()

@kết thúc

@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);

}
@kết thúc

câu trả lời hay nhất

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

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

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

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

28 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress