ios UICollectionView使用

embedded/2025/2/22 20:47:15/

UICollectionView列表视图和UITableView用法类似,直接给出代码

//
//  myUICollectionViewTestController.m
//  iosstudy2024
//
//  Created by figo on 2025/1/21.
//#import "UICollectionViewTestController.h"@interface UICollectionViewTestController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (strong, nonatomic) IBOutlet UICollectionView *myUICollectionView;@end@implementation UICollectionViewTestController- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view from its nib.
//    self.myUICollectionView.dataSource=self;
//    self.myUICollectionView.delegate=self;/* 纯代码方式写   **/// 设置 myUICollectionView 的布局UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];// 初始化 myUICollectionViewself.myUICollectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];self.myUICollectionView.backgroundColor = [UIColor whiteColor];  // 设置背景色self.myUICollectionView.delegate = self;self.myUICollectionView.dataSource = self;// 注册 myUICollectionViewCell[self.myUICollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"MyCell"];// 将 myUICollectionView 添加到视图中[self.view addSubview:self.myUICollectionView];}/*
#pragma mark - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {// Get the new view controller using [segue destinationViewController].// Pass the selected object to the new view controller.
}
*/- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];cell.contentView.backgroundColor = [self randomColor];// 配置 cell,比如设置 label 文本return cell;
}- (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {return 12;
}- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {NSLog(@"Selected item at index %ld", (long)indexPath.item);
}- (UIColor *)randomColor {CGFloat red = (CGFloat)arc4random_uniform(256) / 255.0;CGFloat green = (CGFloat)arc4random_uniform(256) / 255.0;CGFloat blue = (CGFloat)arc4random_uniform(256) / 255.0;return [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
}@end


http://www.ppmy.cn/embedded/164091.html

相关文章

阿里云虚机的远程桌面登录提示帐户被锁定了

提示由于安全原因&#xff0c;帐户被锁定。 阿里云虚机ECS的远程桌面登录提示帐户被锁定了&#xff0c;只能登录阿里云处理 阿里云-计算&#xff0c;为了无法计算的价值 需选择通过VNC连接 然后计算机管理&#xff0c;解除帐户锁定即可。

DeepSeek R1生成图片总结2(虽然本身是不能直接生成图片,但是可以想办法利用别的工具一起实现)

DeepSeek官网 目前阶段&#xff0c;DeepSeek R1是不能直接生成图片的&#xff0c;但可以通过优化文本后转换为SVG或HTML代码&#xff0c;再保存为图片。另外&#xff0c;Janus-Pro是DeepSeek的多模态模型&#xff0c;支持文生图&#xff0c;但需要本地部署或者使用第三方工具。…

phpmyadmin 文件包含(CVE-2014-8959)

目录 漏洞描述 1.攻击机配置go环境&#xff1a; 2.点击“数据库”抓包token值 3.拿到flag&#xff1a; 漏洞描述 phpMyAdmin是一套开源的、基于Web的MySQL数据库管理工具。其index.php中存在一处文件包含逻辑&#xff0c;通过二次编码即可绕过检查&#xff0c;造成远程文件…

css里flex+margin布局

css里flexmargin布局 居中两端排列依次排列 在flexmargin的布局中&#xff0c;margin设置auto会自动将元素剩余的空间用margin填满 居中 <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>居中</tit…

【ISO 14229-1:2023 UDS诊断(ECU复位0x11服务)测试用例CAPL代码全解析⑰】

ISO 14229-1:2023 UDS诊断【ECU复位0x11服务】_TestCase17 作者&#xff1a;车端域控测试工程师 更新日期&#xff1a;2025年02月19日 关键词&#xff1a;UDS诊断协议、ECU复位服务、0x11服务、ISO 14229-1:2023 TC11-017测试用例 用例ID测试场景验证要点参考条款预期结果TC…

网络安全入门攻击与防御实战(四)

漏洞利用&#xff1a;永恒之蓝&#xff08;MS17-010&#xff09;与同类漏洞解析 1 永恒之蓝&#xff08;MS17-010&#xff09;漏洞背景 &#xff08;1&#xff09;漏洞信息 CVE编号&#xff1a;CVE-2017-0143 ~ CVE-2017-0148 影响范围&#xff1a;Windows XP ~ Windows 201…

探索飞鹤奶粉奥秘,领会科技魅力

在科技迅猛发展的当下&#xff0c;AI 技术正深刻重塑各行业格局。乳制品行业亦不例外。近日&#xff0c;长江商学院「AI 未来空间站」的同学们深入走访了飞鹤集团&#xff0c;探寻其在数字化浪潮中的创新实践与卓越成就。 在飞鹤的智能化生产车间&#xff0c;同学们目睹了高度自…

深入解析SORT多目标跟踪算法:从原理到实现

深入解析SORT多目标跟踪算法&#xff1a;从原理到实现 一、多目标跟踪概述 1.1 问题定义 多目标跟踪&#xff08;Multiple Object Tracking, MOT&#xff09;是计算机视觉领域的核心任务之一&#xff0c;旨在从视频序列中持续检测多个目标并维护其身份标识。其核心挑战在于处…