-
Notifications
You must be signed in to change notification settings - Fork 899
/
AllTest9ViewController.m
133 lines (96 loc) · 4.06 KB
/
AllTest9ViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//
// AllTest9ViewController.m
// MyLayoutDemo
//
// Created by oubaiquan on 2017/10/31.
// Copyright © 2017年 YoungSoft. All rights reserved.
//
#import "AllTest9ViewController.h"
#import "AllTest9CollectionViewCell.h"
#import "AllTest9WaterFlowLayout.h"
@interface AllTest9ViewController ()
@property(nonatomic, strong) NSMutableArray *datas; //数据模型数组
@end
@implementation AllTest9ViewController
static NSString * const reuseIdentifier = @"Cell";
-(instancetype)init
{
AllTest9WaterFlowLayout *layout = [[AllTest9WaterFlowLayout alloc] init];
layout.numberOfColumn = 2;
layout.vertSpace = 10;
layout.horzSpace = 10;
//您可以试试内置的UICollectionViewFlowLayout效果。
/*
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.minimumLineSpacing = 10;
layout.minimumInteritemSpacing = 10;
layout.estimatedItemSize = UICollectionViewFlowLayoutAutomaticSize;
*/
self = [self initWithCollectionViewLayout:layout];
if (self != nil) {
}
return self;
}
-(NSMutableArray*)datas
{
if (_datas == nil)
{
_datas = [NSMutableArray new];
NSArray *titles = @[@"欧阳大哥",
@"醉里挑灯看键",
@"张三",
@"尼古拉・阿列克赛耶维奇・奥斯特洛夫斯基"
];
NSArray *subtitles = @[@"只有一行文本",
@"这个例子是用于测试自动布局在UITableView中实现静态高度的解决方案。",
@"通过布局视图的sizeThatFits函数能够评估出UITableViewCell的动态高度",
@"这是一段既有文本也有图片,文本在上面,图片在下面"
];
for (int i = 0; i < 50; i++)
{
AllTest9DataModel *model = [AllTest9DataModel new];
model.title = titles[arc4random_uniform((uint32_t)titles.count)];
model.subtitle = subtitles[arc4random_uniform((uint32_t)subtitles.count)];
[_datas addObject:model];
}
}
return _datas;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.collectionView.backgroundColor = [UIColor whiteColor];
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = NO;
// Register cell classes
[self.collectionView registerClass:[AllTest9CollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#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.
}
*/
#pragma mark <UICollectionViewDataSource>
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake((collectionView.frame.size.width - 10)/2, 0);
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 50;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
AllTest9CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
AllTest9DataModel *model = self.datas[indexPath.section * 1 + indexPath.row];
cell.model = model;
return cell;
}
@end