ios UICollectionView使用

打印 上一主题 下一主题

主题 900|帖子 900|积分 2700

UICollectionView列表视图和UITableView用法类似,直接给出代码
  1. //
  2. //  myUICollectionViewTestController.m
  3. //  iosstudy2024
  4. //
  5. //  Created by figo on 2025/1/21.
  6. //
  7. #import "UICollectionViewTestController.h"
  8. @interface UICollectionViewTestController ()<UICollectionViewDelegate,UICollectionViewDataSource>
  9. @property (strong, nonatomic) IBOutlet UICollectionView *myUICollectionView;
  10. @end
  11. @implementation UICollectionViewTestController
  12. - (void)viewDidLoad {
  13.     [super viewDidLoad];
  14.     // Do any additional setup after loading the view from its nib.
  15. //    self.myUICollectionView.dataSource=self;
  16. //    self.myUICollectionView.delegate=self;
  17.    
  18.     /* 纯代码方式写   **/
  19.     // 设置 myUICollectionView 的布局
  20.       UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  21.       
  22.       // 初始化 myUICollectionView
  23.       self.myUICollectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
  24.       self.myUICollectionView.backgroundColor = [UIColor whiteColor];  // 设置背景色
  25.       self.myUICollectionView.delegate = self;
  26.       self.myUICollectionView.dataSource = self;
  27.       
  28.       // 注册 myUICollectionViewCell
  29.       [self.myUICollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"MyCell"];
  30.       
  31.       // 将 myUICollectionView 添加到视图中
  32.       [self.view addSubview:self.myUICollectionView];
  33.    
  34. }
  35. /*
  36. #pragma mark - Navigation
  37. // In a storyboard-based application, you will often want to do a little preparation before navigation
  38. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  39.     // Get the new view controller using [segue destinationViewController].
  40.     // Pass the selected object to the new view controller.
  41. }
  42. */
  43. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  44.     UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];
  45.     cell.contentView.backgroundColor = [self randomColor];
  46.     // 配置 cell,比如设置 label 文本
  47.     return cell;
  48. }
  49. - (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  50.     return 12;
  51. }
  52. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  53.     NSLog(@"Selected item at index %ld", (long)indexPath.item);
  54. }
  55. - (UIColor *)randomColor {
  56.     CGFloat red = (CGFloat)arc4random_uniform(256) / 255.0;
  57.     CGFloat green = (CGFloat)arc4random_uniform(256) / 255.0;
  58.     CGFloat blue = (CGFloat)arc4random_uniform(256) / 255.0;
  59.    
  60.     return [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
  61. }
  62. @end
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

商道如狼道

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表