UICollectionView oc显示2行示例代码
时间: 2023-03-27 10:04:27 浏览: 71
以下是 UICollectionView 的 Objective-C 示例代码,用于显示两行:
```
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 6;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 2;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
cell.backgroundColor = [UIColor whiteColor];
return cell;
}
```
以上是示例代码,具体实现还需要根据实际需求进行调整。
相关问题
UICollectionView oc示例代码
以下是 UICollectionView 的示例代码,使用 Objective-C 语言编写:
// 创建 UICollectionViewFlowLayout 对象
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
// 设置每个 item 的大小
layout.itemSize = CGSizeMake(100, 100);
// 设置 item 之间的间距
layout.minimumInteritemSpacing = 10;
// 设置行之间的间距
layout.minimumLineSpacing = 10;
// 设置 UICollectionView 的滚动方向为垂直方向
layout.scrollDirection = UICollectionViewScrollDirectionVertical;
// 创建 UICollectionView 对象
UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
// 设置 UICollectionView 的背景色
collectionView.backgroundColor = [UIColor whiteColor];
// 设置 UICollectionView 的数据源和代理
collectionView.dataSource = self;
collectionView.delegate = self;
// 注册 UICollectionViewCell 类
[collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
// 将 UICollectionView 添加到视图中
[self.view addSubview:collectionView];
// 实现 UICollectionViewDataSource 协议中的方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 10;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
cell.backgroundColor = [UIColor redColor];
return cell;
}
以上是 UICollectionView 的示例代码,希望能对你有所帮助。
UICollectionView oc完整示例代码
以下是一个 UICollectionView 的完整示例代码,供您参考:
```
// 在 ViewController.h 中声明 UICollectionViewDelegate 和 UICollectionViewDataSource 协议
@interface ViewController : UIViewController <UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *collectionView;
@end
// 在 ViewController.m 中实现 UICollectionViewDelegate 和 UICollectionViewDataSource 协议
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 创建 UICollectionViewFlowLayout
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.itemSize = CGSizeMake(100, 100);
layout.minimumLineSpacing = 10;
layout.minimumInteritemSpacing = 10;
layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
// 创建 UICollectionView
self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
[self.view addSubview:self.collectionView];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 10;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
cell.backgroundColor = [UIColor redColor];
return cell;
}
@end
```
希望对您有所帮助!