问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

用uicollectionview怎么实现网格效果图

发布网友 发布时间:2022-04-22 02:35

我来回答

1个回答

热心网友 时间:2023-06-30 02:22

KMCollectionViewCell.h

1 #import <UIKit/UIKit.h>
2
3 @interface KMCollectionViewCell : UICollectionViewCell
4 @property (strong, nonatomic) IBOutlet UIImageView *imgVCustom;
5 @property (strong, nonatomic) IBOutlet UILabel *lblDesc;
6
7 @end

KMCollectionViewCell.m

1 #import "KMCollectionViewCell.h"
2
3 @implementation KMCollectionViewCell
4
5 - (id)initWithFrame:(CGRect)frame { //由于没调用,所以不会触发initWithFrame方法
6 if (self = [super initWithFrame:frame]) {
7 //Some codes as the content of drawRect's method
8 }
9 return self;
10 }
11
12 - (void)drawRect:(CGRect)rect { //drawRect方法会在每次对象实例化时调用
13 _imgVCustom.layer.masksToBounds = YES;
14 _imgVCustom.layer.cornerRadius = _imgVCustom.frame.size.width/2.0; //设置圆角;当值为正方形图片视图的宽度一半时,就为圆形
15
16 _lblDesc.adjustsFontSizeToFitWidth = YES; //设置是否根据内容自适应调整字体大小;默认值为NO
17 }
18
19 @end

ViewController.h

1 #import <UIKit/UIKit.h>
2
3 @interface ViewController : UIViewController <UICollectionViewDataSource, UICollectionViewDelegate>
4 @property (strong, nonatomic) NSMutableArray *mArrData;
5 @property (strong, nonatomic) IBOutlet UICollectionView *collVCustom;
6
7 @end

ViewController.m

1 #import "ViewController.h"
2
3 #import "KMCollectionViewCell.h"
4 @interface ViewController ()
5 - (void)layoutUI;
6 @end
7
8 @implementation ViewController
9
10 #define kNumberOfImage 9
11 #define kNumberOfCells 25
12 #define kNumberOfColumns 3
13 #define kPaddingOfScreen 20.0
14
15 - (void)viewDidLoad {
16 [super viewDidLoad];
17
18 [self layoutUI];
19 }
20
21 - (void)didReceiveMemoryWarning {
22 [super didReceiveMemoryWarning];
23 // Dispose of any resources that can be recreated.
24 }
25
26 - (void)layoutUI {
27 self.view.backgroundColor = [UIColor whiteColor];
28 self.navigationItem.title = @"使用UICollectionView实现网格化视图效果";
29
30 //填充作为数据源的可变长数组_mArrData的数据
31 _mArrData = [[NSMutableArray alloc] initWithCapacity:kNumberOfCells];
32 for (NSUInteger i=0; i<kNumberOfCells; i++) {
33 UIImage *imgCustom = [UIImage imageNamed:[NSString stringWithFormat:@"%lu", (i%kNumberOfImage + 1)]];
34 NSString *strDesc = [NSString stringWithFormat:@"第%lu张照片", (i+1)];
35 //NSDictionary *dicData = @{ @"image":imgCustom, @"desc":strDesc }; //考虑字典是否是可变的,如果需要修改里面的键值对的话,建议用NSMutableDictionary
36
37 //NSMutableDictionary *mDicData = [NSMutableDictionary dictionaryWithObjectsAndKeys:imgCustom, @"image", strDesc, @"desc", nil];
38 NSMutableDictionary *mDicData =[[NSMutableDictionary alloc] initWithDictionary:@{ @"image":imgCustom, @"desc":strDesc }];
39 [_mArrData addObject:mDicData];
40 }
41
42 _collVCustom.dataSource = self;
43 _collVCustom.delegate = self;
44 //NSLog(@"%0.2f, %0.2f", _collVCustom.frame.size.width, _collVCustom.frame.size.height);
45 }
46
47 #pragma mark - CollectionView : UICollectionViewDataSource
48 - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
49 return 1;
50 }
51
52 - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
53 return kNumberOfCells;
54 }
55
56 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
57 static NSString *cellIdentifier = @"cellIdentifier";
58 KMCollectionViewCell *cell = (KMCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
59
60 NSMutableDictionary *mDicData = _mArrData[indexPath.row];
61 cell.imgVCustom.image = mDicData[@"image"];
62 cell.lblDesc.text = mDicData[@"desc"];
63 return cell;
64 }
65
66 - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
67 // The view that is returned must be retrieved from a call to -dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath:
68 return nil;
69 }
70
71 #pragma mark - CollectionView : UICollectionViewDelegate
72 - (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
73 return YES;
74 }
75
76 - (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
77 NSLog(@"didHighlightItemAtIndexPath:, indexPath.row=%ld ", (long)indexPath.row);
78 }
79
80 - (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath {
81 NSLog(@"didUnhighlightItemAtIndexPath:, indexPath.row=%ld ", (long)indexPath.row);
82 }
83
84 - (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
85 return YES;
86 }
87
88 - (BOOL)collectionView:(UICollectionView *)collectionView shouldDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
89 return YES;
90 }
91
92 - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
93 NSMutableDictionary *mDicData = _mArrData[indexPath.row];
94 mDicData[@"desc"] = @"你点击了我"; //[mDicData setValue:@"你点击了我" forKey:@"desc"];
95 [collectionView reloadItemsAtIndexPaths:@[indexPath]];
96
97 NSLog(@"didSelectItemAtIndexPath:, indexPath.row=%ld ", (long)indexPath.row);
98 }
99
100 - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
101 //在didSelectItemAtIndexPath执行了reloadItemsAtIndexPaths会导致didDeselectItemAtIndexPath失效不执行,所以以下打印的语句不会执行
102
103 NSLog(@"didDeselectItemAtIndexPath:, indexPath.row=%ld ", (long)indexPath.row);
104 }
105
106 /*
107 #pragma mark - CollectionView : UICollectionViewDelegateFlowLayout
108 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
109 return CGSizeMake(80.0, 120.0);
110 }
111
112 - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
113 return UIEdgeInsetsMake(5.0, 5.0, 5.0, 5.0);
114 }
115
116 - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
117 return 10.0;
118 }
119
120 - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
121 return 10.0;
122 }
123
124 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
125 return CGSizeMake(5.0, 5.0);
126 }
127
128 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
129 return CGSizeMake(5.0, 5.0);
130 }
131 */
132
133 @end
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
临沂比较有名的男装品牌 呼伦贝尔市悦动网络科技有限公司怎么样? 呼伦贝尔中汇实业有限公司怎么样? 呼伦贝尔油玉不绝电子商务有限公司怎么样? 如何避免wps卡顿? 属鼠的男人找对象是属什么,属鼠的人和什么属相合 96年鼠的姻缘在哪年 属相相合年份运势提升 2024属鼠找对象属什么最佳 黑客攻击网站能报案吗 黑客攻击报案有用吗 swift uiimage怎么改大小 ios11 中uicollectionview reloaddata 为什么页面... semantic ui要装什么才能使用 如何在UIImageView上画线和画圆 什么叫uiimage的渲染模式 怎么调整UIImage的大小? 携程上的旅游订制有人用过没?好用么? UIImage加载图片的区别和渲染模式 请问携程的私家团和普通的团队游有什么差别? 如何用 C++ 从零编写 GUI 携程里的日上靠谱吗? 为什么Unity3D能够产生如此多的插件 如何用C或C++编写一个自然的IMGUI 携程网靠谱吗? 如何设计类似Unity3D使用的ImGui框架的API 携程上的一日游靠谱吗?经常听到报道说一日游强制... imgui内存不能为write 无量跌停预示着什么 imgui 中vector4的参数分别表示什么 携程旅游的私家团和跟团游有什么区别? 使用Bootstrap.css写html页面,如何使img在浏览器... CGContextRef怎么清除前一次的绘制的图形 智慧经济包含哪些内容 集约型经济与智慧经济的联系与区别 智慧经济的智慧经济的产生过程 智慧经济的特征 智慧经济的智慧经济是创新驱动型经济 智慧经济的定义 什么是智慧经济 智慧经济是谁提出的 什么是产业智慧化?包括哪些方面? 构成智慧城市的核心有哪些 知识经济和智慧经济是一回事么 将有3亿人口从农村迁入,智慧城市该怎么建 如何实现智慧城市可持续发展 智慧经济的释义 智慧城市发展现状如何? 智慧城市包括哪些方面? 真空包装的牛排要洗吗 在超市买的密封装的牛排需要洗吗