ios UICollectionView使用自定义UICollectionViewCell

embedded/2025/2/26 3:34:52/

和UITableView用法类似,UITableView主要是显示按行排列的数据,UICollectionView则用在显示多行多列的数据,今天我们继续来实现app下载页面的效果。

1.先自定义UICollectionViewCell,一个cell就相当于列表中的一项了。

记得勾上,这样就自动创建xib组件了

按住ctrl,鼠标连线到AppCollectionView.h文件,定义好属性

AppCollectionViewCell.h

//
//  AppCollectionViewCell.h
//  iosstudy2024
//
//  Created by figo on 2025/2/18.
//#import <UIKit/UIKit.h>NS_ASSUME_NONNULL_BEGIN@interface AppCollectionViewCell : UICollectionViewCell
@property (weak, nonatomic) IBOutlet UIImageView *appImg;
@property (weak, nonatomic) IBOutlet UILabel *appName;
@property (weak, nonatomic) IBOutlet UIButton *btnDownload;@endNS_ASSUME_NONNULL_END

AppCollectionViewCell.m

//
//  AppCollectionViewCell.m
//  iosstudy2024
//
//  Created by figo on 2025/2/18.
//#import "AppCollectionViewCell.h"@implementation AppCollectionViewCell- (void)awakeFromNib {[super awakeFromNib];// Initialization code
}@end

2.创建控制器AppsDownViewController

AppsDownViewController.h

//
//  AppsDownViewController.h
//  iosstudy2024
//
//  Created by figo on 2025/2/18.
//#import <UIKit/UIKit.h>NS_ASSUME_NONNULL_BEGIN@interface AppsDownViewController : UIViewController@endNS_ASSUME_NONNULL_END

AppsDownViewController.m

//
//  AppsDownViewController.m
//  iosstudy2024
//
//  Created by figo on 2025/2/18.
//
#import "AppCollectionViewCell.h"
#import "AppsDownViewController.h"@interface AppsDownViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet UICollectionView *appUICollectionView;@end@implementation AppsDownViewController- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view from its nib.self.appUICollectionView.delegate = self;self.appUICollectionView.dataSource = self;//关联自定义AppCollectionViewCell到当前页面的UICollectionViewUINib * nib=[UINib nibWithNibName:@"AppCollectionViewCell" bundle:nil];[self.appUICollectionView registerNib:nib forCellWithReuseIdentifier:@"testCell"];//设置布局格式UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init];layout.scrollDirection = UICollectionViewScrollDirectionVertical;
//    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;//    CGFloat w=(UIScreen.mainScreen.bounds.size.width-3*30)/2;layout.itemSize=CGSizeMake(101, 135);
//    layout.minimumLineSpacing=5;
//    layout.minimumInteritemSpacing=5;
//    layout.sectionInset=UIEdgeInsetsMake(0, 5, 0, 5);self.appUICollectionView.collectionViewLayout=layout;self.appUICollectionView.backgroundColor=[UIColor blueColor];
}//每一项collectionViewCell
- (nonnull __kindof UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {AppCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"testCell" forIndexPath:indexPath];//使用tag传参@"APPName"+indexPath.item;cell.appName.text=[NSString stringWithFormat:@"APPName%ld",indexPath.item];NSString *imgPath=[[NSBundle mainBundle]pathForResource:@"star" ofType:@".png"];//照片拖入Assets件夹会找不到资源,注意需要项目下新建group命名为Supporting Files,再项目外新建文件夹比如icons,然后将图片放入icons,再将icons文件夹拖入Supporting Files才能找到,否则返回nilUIImage *uiImage=[UIImage imageWithContentsOfFile:imgPath];cell.appImg.image=uiImage;cell.btnDownload.tag=indexPath.item;//给按钮添加事件[cell.btnDownload addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];return cell;
}
-(void)btnClick:(UIButton *) btn  {NSLog(@"Selected item at index %ld", btn.tag);
}//选中某一项
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {NSLog(@"didSelectItemAtIndexPath %ld", (long)indexPath.item);
}//每个Section里面有多少项
- (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {return 12;
}//定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{return 1;
}
@end

xib文件,就放了一个UICollectionView

3.SceneDelegate.m设置运行当前controller

//
//  SceneDelegate.m
//  iosstudy2024
//
//  Created by figo on 2024/8/5.
//#import "SceneDelegate.h"
//#import "WidgetViewController.h"
//#import "TableViewTestViewController.h"
//#import "TableViewAddressBookViewController.h"
#import "NewsViewController.h"
#import "UICollectionViewTestController.h"
#import "AppDownloadViewController.h"
#import "AppsDownViewController.h"
@interface SceneDelegate ()@end@implementation SceneDelegate- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.AppsDownViewController * viewController = [[AppsDownViewController alloc]init];self.window.rootViewController=viewController;
}- (void)sceneDidDisconnect:(UIScene *)scene {// Called as the scene is being released by the system.// This occurs shortly after the scene enters the background, or when its session is discarded.// Release any resources associated with this scene that can be re-created the next time the scene connects.// The scene may re-connect later, as its session was not necessarily discarded (see `application:didDiscardSceneSessions` instead).
}- (void)sceneDidBecomeActive:(UIScene *)scene {// Called when the scene has moved from an inactive state to an active state.// Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.NSLog(@"%s",__func__);}- (void)sceneWillResignActive:(UIScene *)scene {// Called when the scene will move from an active state to an inactive state.// This may occur due to temporary interruptions (ex. an incoming phone call).NSLog(@"%s",__func__);
}- (void)sceneWillEnterForeground:(UIScene *)scene {// Called as the scene transitions from the background to the foreground.// Use this method to undo the changes made on entering the background.NSLog(@"%s",__func__);
}- (void)sceneDidEnterBackground:(UIScene *)scene {// Called as the scene transitions from the foreground to the background.// Use this method to save data, release shared resources, and store enough scene-specific state information// to restore the scene back to its current state.NSLog(@"%s",__func__);
}@end


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

相关文章

Web自动化之Selenium添加网站Cookies实现免登录

在使用Selenium进行Web自动化时&#xff0c;添加网站Cookies是实现免登录的一种高效方法。通过模拟浏览器行为&#xff0c;我们可以将已登录状态的Cookies存储起来&#xff0c;并在下次自动化测试或爬虫任务中直接加载这些Cookies&#xff0c;从而跳过登录步骤。 Cookies简介 …

关于 Grok-3 大语言模型的研究

摘要:本文深入研究埃隆・马斯克旗下 xAI 团队研发的大语言模型 Grok-3。Grok-3 依托强大的超级计算基础设施,采用独特训练数据策略与创新模型架构,在性能指标、功能特性及应用场景展现出显著优势,同时也引发技术争议与行业格局变动,对人工智能发展影响深远。 关键词:Grok…

百度百舸 DeepSeek 一体机发布,支持昆仑芯 P800 单机 8 卡满血版开箱即用

在私有云环境中成功部署 DeepSeek 满血版并实现性能调优&#xff0c;并不是一件容易的事情。选择合适的 GPU 配置、安装相应的环境、成功部署上线业务、加速推理任务加速、支撑多用户并发 …… 完成业务测试&#xff0c;成功融入生产业务中。 为了帮助企业快速实现 DeepSeek 服…

如何排查服务器 DNS 解析失败的问题

DNS&#xff08;Domain Name System&#xff09;解析是将域名转换为 IP 地址的过程。DNS 解析失败会导致服务器无法访问外部资源或用户无法访问服务器。以下是详细的排查步骤和方法。 1. 确认问题现象 首先&#xff0c;明确问题的具体表现&#xff1a; 服务器无法访问特定域名…

AI知识架构之AIGC

AIGC 基础概念 定义与范畴 定义:AIGC 即 Artificial Intelligence Generated Content,指利用人工智能技术生成内容。这意味着人工智能不再仅仅是分析或处理现有数据,而是能够主动创造出文本、图像、音频、视频等各种形式的内容。范畴:其涵盖范围广泛,涉及多模态内容。文本…

Python常见面试题的详解16

1. 如何强行关闭客户端和服务器之间的连接&#xff1f; 在网络编程中&#xff0c;有时需要强行中断客户端和服务器之间的连接。对于基于 TCP 协议的连接&#xff0c;由于其面向连接的特性&#xff0c;需要采取特定的步骤来确保连接被正确关闭&#xff1b;而 UDP 是无连接协议&a…

51单片机-AT24CXX存储器工作原理

1、AT24CXX存储器工作原理 1.1、特点&#xff1a; 与400KHz&#xff0c;I2C总线兼容1.8到6.0伏工作电压范围低功耗CMOS技术写保护功能当WP为高电平时进入写保护状态页写缓冲器自定时擦写周期100万次编程/擦除周期可保存数据100年8脚DIP SOIC或TSSOP封装温度范围商业级和工业级…

Llama 3.1 本地电脑部署 Linux系统 【轻松简易】

本文分享在自己的本地电脑部署 llama3.1&#xff0c;而且轻松简易&#xff0c;快速上手。 这里借助Ollama工具&#xff0c;在Linux系统中进行大模型部署~ Llama3.1&#xff0c;有三个版本&#xff1a;8B、70B、405B Llama 3.1 405B 是第一个公开可用的模型&#xff0c;在常识…