Skip to content

Commit 2926596

Browse files
肖睿肖睿
肖睿
authored and
肖睿
committed
修复数据源改变后,刷新崩溃的bug
1 parent bc176df commit 2926596

File tree

4 files changed

+19
-10
lines changed

4 files changed

+19
-10
lines changed

XRWaterfallLayout/XRWaterfallLayout.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,15 @@ - (void)setColumnSpacing:(NSInteger)columnSpacing rowSpacing:(NSInteger)rowSepac
5959
#pragma mark- 布局相关方法
6060
//布局前的准备工作
6161
- (void)prepareLayout {
62+
[super prepareLayout];
6263
//初始化字典,有几列就有几个键值对,key为列,value为列的最大y值,初始值为上内边距
6364
for (int i = 0; i < self.columnCount; i++) {
6465
self.maxYDic[@(i)] = @(self.sectionInset.top);
6566
}
6667

6768
//根据collectionView获取总共有多少个item
6869
NSInteger itemCount = [self.collectionView numberOfItemsInSection:0];
69-
70+
[self.attributesArray removeAllObjects];
7071
//为每一个item创建一个attributes并存入数组
7172
for (int i = 0; i < itemCount; i++) {
7273
UICollectionViewLayoutAttributes *attributes = [self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0]];

XRWaterfallLayoutDemo/XRWaterfallLayoutDemo/ViewController.m

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,15 @@ - (NSMutableArray *)images {
3232

3333
- (void)viewDidLoad {
3434
[super viewDidLoad];
35+
3536
//创建瀑布流布局
36-
//方式一
37-
// XRWaterfallLayout *waterfall = [[XRWaterfallLayout alloc] init];
38-
// waterfall.columnCount = 3;
39-
//方式二
40-
// XRWaterfallLayout *waterfall = [[XRWaterfallLayout alloc] initWithColumnCount:3];
41-
//方式三
4237
XRWaterfallLayout *waterfall = [XRWaterfallLayout waterFallLayoutWithColumnCount:3];
4338

4439
//设置各属性的值
4540
// waterfall.rowSpacing = 10;
4641
// waterfall.columnSpacing = 10;
4742
// waterfall.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
43+
4844
//或者一次性设置
4945
[waterfall setColumnSpacing:10 rowSpacing:10 sectionInset:UIEdgeInsetsMake(10, 10, 10, 10)];
5046

@@ -62,10 +58,21 @@ - (void)viewDidLoad {
6258

6359
//创建collectionView
6460
self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:waterfall];
61+
self.collectionView.frame = CGRectMake(0, 50, self.view.frame.size.width, self.view.frame.size.height - 50);
6562
self.collectionView.backgroundColor = [UIColor whiteColor];
6663
[self.collectionView registerNib:[UINib nibWithNibName:@"XRCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];
6764
self.collectionView.dataSource = self;
6865
[self.view addSubview:self.collectionView];
66+
67+
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(100, 20, 100, 30)];
68+
[btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
69+
btn.backgroundColor = [UIColor blueColor];
70+
[self.view addSubview:btn];
71+
}
72+
73+
- (void)click {
74+
[self.images removeAllObjects];
75+
[self.collectionView reloadData];
6976
}
7077

7178

XRWaterfallLayoutDemo/XRWaterfallLayoutDemo/XRCollectionViewCell.xib

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="9531" systemVersion="15C50" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES">
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="9532" systemVersion="15D21" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES">
33
<dependencies>
44
<deployment identifier="iOS"/>
5-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="9529"/>
5+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="9530"/>
66
</dependencies>
77
<objects>
88
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/>

XRWaterfallLayoutDemo/XRWaterfallLayoutDemo/XRWaterfallLayout/XRWaterfallLayout.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,15 @@ - (void)setColumnSpacing:(NSInteger)columnSpacing rowSpacing:(NSInteger)rowSepac
5959
#pragma mark- 布局相关方法
6060
//布局前的准备工作
6161
- (void)prepareLayout {
62+
[super prepareLayout];
6263
//初始化字典,有几列就有几个键值对,key为列,value为列的最大y值,初始值为上内边距
6364
for (int i = 0; i < self.columnCount; i++) {
6465
self.maxYDic[@(i)] = @(self.sectionInset.top);
6566
}
6667

6768
//根据collectionView获取总共有多少个item
6869
NSInteger itemCount = [self.collectionView numberOfItemsInSection:0];
69-
70+
[self.attributesArray removeAllObjects];
7071
//为每一个item创建一个attributes并存入数组
7172
for (int i = 0; i < itemCount; i++) {
7273
UICollectionViewLayoutAttributes *attributes = [self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0]];

0 commit comments

Comments
 (0)