Skip to content

Commit 371c319

Browse files
author
pifupro_iOS_admin
committed
code restructuring
1 parent 180fa67 commit 371c319

File tree

1 file changed

+51
-47
lines changed

1 file changed

+51
-47
lines changed

CollectionViewClassifyMenu/Class/ClassifyMenu(两级可折叠菜单)/Controller/CYLClassifyMenuViewController.m

Lines changed: 51 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,12 @@ - (void)viewDidLoad {
103103

104104
[self initData];
105105
[self addCollectionView];
106-
[self judgeMoreBtnShow];
106+
[self judgeMoreButtonShow];
107107
[self.backgroundView addSubview:[self addTableHeaderView]];
108108
self.view.backgroundColor = [UIColor blueColor];
109109
//如果想显示两行,请打开下面两行代码,(这两行代码必须在“[self addTableHeaderView]”之后)
110-
// self.showLineSwitch.on = YES;
111-
// [self showLineSwitchClicked:self.showLineSwitch];
110+
// self.showLineSwitch.on = YES;
111+
// [self showLineSwitchClicked:self.showLineSwitch];
112112
}
113113

114114
-(void)viewDidAppear:(BOOL)animated {
@@ -149,31 +149,44 @@ - (float)cellLimitWidth:(float)cellWidth
149149
return cellWidth;
150150
}
151151

152-
- (float)textAndImageWidth:(NSString *)text
153-
content:(id)obj
154-
array:(NSArray *)array {
152+
/*
153+
分行规则:cell与cell之间必须有大小为kCollectionViewCellsHorizonMargin的间距,左右可以没有间距。
154+
=》》一旦cell+kCollectionViewCellsHorizonMargin超过contentViewWidth,则肯定要分行。cell超过contentViewWidth也会分行。两者的区别在于cell的宽度,前者还是自身宽度,但后者已经变成了contentViewWidth的宽度。
155+
*/
156+
- (float)textImageWidthAndRightMargin:(NSString *)text
157+
content:(id)obj
158+
array:(NSArray *)array {
155159
CGFloat contentViewWidth = CGRectGetWidth(self.collectionView.frame) - kCollectionViewToLeftMargin - kCollectionViewToRightMargin;
156160
__block float cellWidth = [self collectionCellWidthText:text content:obj];
157161
__block float cellWidthAndRightMargin;
158-
[self cellLimitWidth:cellWidth
159-
limitMargin:kCollectionViewCellsHorizonMargin
160-
isLimitWidth:^(BOOL yesORNo, NSNumber *data) {
161-
cellWidth = [data floatValue];
162-
if (yesORNo == YES) {
163-
cellWidthAndRightMargin = contentViewWidth;
164-
} else {
165-
if (obj == [array lastObject]) {
166-
cellWidthAndRightMargin = cellWidth;
167-
} else {
168-
cellWidthAndRightMargin = cellWidth + kCollectionViewCellsHorizonMargin;
169-
}
170-
}
171-
}];
162+
163+
if(cellWidth == contentViewWidth) {
164+
cellWidthAndRightMargin = contentViewWidth;
165+
} else {
166+
if (obj == [array lastObject]) {
167+
cellWidthAndRightMargin = cellWidth;
168+
} else {
169+
[self cellLimitWidth:cellWidth
170+
limitMargin:kCollectionViewCellsHorizonMargin
171+
isLimitWidth:^(BOOL isLimitWidth, NSNumber *data) {
172+
if (isLimitWidth) {
173+
//当cell和kCollectionViewCellsHorizonMargin总和大于contentViewWidth,
174+
//但是cell却小于contentViewWidth时,还是占一行。
175+
cellWidthAndRightMargin = contentViewWidth;
176+
} else {
177+
//这个地方只是大概的估计下,他不能判断出当加上下一个cell的cellWidthAndRightMargin大于contentViewWidth时,
178+
//cellWidthAndRightMargin右侧剩余的部分
179+
//所以必须在后续判断与下个一cell的cellWidthAndRightMargin的和超出contentViewWidth时的情况
180+
cellWidthAndRightMargin = cellWidth + kCollectionViewCellsHorizonMargin;
181+
}
182+
}];
183+
}
184+
}
172185
return cellWidthAndRightMargin;
173186
}
174187

175-
- (void)judgeMoreBtnShow {
176-
CGFloat contentViewWidth = self.collectionView.frame.size.width -
188+
- (void)judgeMoreButtonShow {
189+
CGFloat contentViewWidth = CGRectGetWidth(self.collectionView.frame) -
177190
kCollectionViewToLeftMargin - kCollectionViewToRightMargin;
178191
NSMutableArray *firstLineWidthArray = [NSMutableArray array];
179192
__weak __typeof(self) weakSelf = self;
@@ -186,9 +199,9 @@ - (void)judgeMoreBtnShow {
186199
__weak __typeof(symptoms) weakSymptoms = symptoms;
187200
[symptoms enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
188201
NSString *text = [obj objectForKey:kDataSourceCellTextKey];
189-
float cellWidthAndRightMargin = [strongSelf textAndImageWidth:text
190-
content:obj
191-
array:weakSymptoms];
202+
float cellWidthAndRightMargin = [strongSelf textImageWidthAndRightMargin:text
203+
content:obj
204+
array:weakSymptoms];
192205
[widthArray addObject:@(cellWidthAndRightMargin)];
193206
NSArray *sumArray = [NSArray arrayWithArray:widthArray];
194207
NSNumber* sum = [sumArray valueForKeyPath: @"@sum.self"];
@@ -211,7 +224,7 @@ - (void)judgeMoreBtnShow {
211224
}];
212225
}
213226

214-
- (void)judgeMoreBtnShowWhenShowTwoRows {
227+
- (void)judgeMoreButtonShowWhenShowTwoRows {
215228
CGFloat contentViewWidth = CGRectGetWidth(self.collectionView.frame) - kCollectionViewToLeftMargin - kCollectionViewToRightMargin;
216229
NSMutableArray *twoRowsWidthArray = [NSMutableArray array];
217230
__weak __typeof(self) weakSelf = self;
@@ -224,9 +237,9 @@ - (void)judgeMoreBtnShowWhenShowTwoRows {
224237
__weak __typeof(symptoms) weakSymptoms = symptoms;
225238
[symptoms enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
226239
NSString *text = [obj objectForKey:kDataSourceCellTextKey];
227-
float cellWidthAndRightMargin = [strongSelf textAndImageWidth:text
228-
content:obj
229-
array:weakSymptoms];
240+
float cellWidthAndRightMargin = [strongSelf textImageWidthAndRightMargin:text
241+
content:obj
242+
array:weakSymptoms];
230243
[widthArray addObject:@(cellWidthAndRightMargin)];
231244
NSMutableArray *sumArray = [NSMutableArray arrayWithArray:widthArray];
232245
NSNumber* sum = [sumArray valueForKeyPath: @"@sum.self"];
@@ -236,16 +249,7 @@ - (void)judgeMoreBtnShowWhenShowTwoRows {
236249
//超过一行时
237250
if(countTime == 0) {
238251
[widthArray removeAllObjects];
239-
[self cellLimitWidth:cellWidthAndRightMargin
240-
limitMargin:kCollectionViewCellsHorizonMargin
241-
isLimitWidth:^(BOOL yesORNo, NSNumber *data) {
242-
if (yesORNo == YES) {
243-
//如果cell宽度或者cell宽度加上间距超过collectionView的contentView宽度,则将cell自成一行
244-
[widthArray addObject:@(contentViewWidth)];
245-
} else {
246-
[widthArray addObject:@(cellWidthAndRightMargin)];
247-
}
248-
}];
252+
[widthArray addObject:@(cellWidthAndRightMargin)];
249253
}
250254
countTime++;
251255
}
@@ -293,9 +297,9 @@ - (NSUInteger)firstLineCellCountWithArray:(NSArray *)array {
293297
[array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
294298
@autoreleasepool {
295299
NSString *text = [obj objectForKey:kDataSourceCellTextKey];
296-
float cellWidthAndRightMargin = [self textAndImageWidth:text
297-
content:obj
298-
array:weakArray];
300+
float cellWidthAndRightMargin = [self textImageWidthAndRightMargin:text
301+
content:obj
302+
array:weakArray];
299303
[widthArray addObject:@(cellWidthAndRightMargin)];
300304
NSArray *sumArray = [NSArray arrayWithArray:widthArray];
301305
NSNumber *sum = [sumArray valueForKeyPath:@"@sum.self"];
@@ -332,7 +336,7 @@ - (UIView *)addTableHeaderView
332336
{
333337
UIView *tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, kControllerHeaderViewHeight)];
334338
tableHeaderView.backgroundColor = [UIColor whiteColor];
335-
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, 35, tableHeaderView.frame.size.width, 20)];
339+
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, 35, CGRectGetWidth(tableHeaderView.frame), 20)];
336340
self.titleLabel = titleLabel;
337341
titleLabel.font = [UIFont boldSystemFontOfSize:18];
338342
titleLabel.textColor = [UIColor colorWithRed:0 green:150.0/255.0 blue:136.0/255.0 alpha:1.0];
@@ -360,7 +364,7 @@ - (UIView *)addTableHeaderView
360364
[showLineSwitch addTarget:self action:@selector(showLineSwitchClicked:) forControlEvents:UIControlEventAllEvents];
361365
self.showLineSwitch = showLineSwitch;
362366
UILabel *subtitleLabel = [[UILabel alloc] init];
363-
subtitleLabel.frame = CGRectMake(titleLabel.frame.origin.x,
367+
subtitleLabel.frame = CGRectMake(CGRectGetMinX(titleLabel.frame),
364368
CGRectGetMaxY(titleLabel.frame) + 10,
365369
[UIScreen mainScreen].bounds.size.width,
366370
14
@@ -413,11 +417,11 @@ - (void)updateViewHeight {
413417

414418
- (void)showLineSwitchClicked:(UISwitch *)sender {
415419
[self initData];
416-
[self judgeMoreBtnShow];
420+
[self judgeMoreButtonShow];
417421
NSString *title;
418422
if(sender.isOn) {
419423
title = @"默认显示两行时的效果如下所示:";
420-
[self judgeMoreBtnShowWhenShowTwoRows];
424+
[self judgeMoreButtonShowWhenShowTwoRows];
421425
[self initDefaultShowCellCount];
422426
} else {
423427
title = @"默认显示一行时的效果如下所示:";
@@ -473,7 +477,7 @@ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
473477
CollectionViewCell *cell =
474478
(CollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:kCellIdentifier
475479
forIndexPath:indexPath];
476-
cell.button.frame = CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height);
480+
cell.button.frame = CGRectMake(0, 0, CGRectGetWidth(cell.frame), CGRectGetHeight(cell.frame));
477481
NSMutableArray *symptoms = [NSMutableArray arrayWithArray:[self.dataSource[indexPath.section]
478482
objectForKey:kDataSourceSectionKey]];
479483
NSString *text = [symptoms[indexPath.row] objectForKey:kDataSourceCellTextKey];

0 commit comments

Comments
 (0)