Skip to content

Commit 2734bef

Browse files
author
pifupro_iOS_admin
committed
1 parent dd0857a commit 2734bef

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ @interface CYLClassifyMenuViewController ()
4949
@property (nonatomic, strong) NSMutableArray *expandSectionArray;
5050
@property (nonatomic, strong) UIScrollView *backgroundView;
5151
@property (nonatomic, strong) UILabel *titleLabel;
52+
@property (nonatomic, strong) UISwitch *showLineSwitch;
5253

5354
@end
5455

@@ -103,11 +104,11 @@ - (void)viewDidLoad {
103104
[self initData];
104105
[self addCollectionView];
105106
[self judgeMoreBtnShow];
106-
// 如果想显示两行,请打开下面两行代码
107-
// [self judgeMoreBtnShowWhenShowTwoRows];
108-
// [self initDefaultShowCellCount];
109107
[self.backgroundView addSubview:[self addTableHeaderView]];
110108
self.view.backgroundColor = [UIColor blueColor];
109+
//如果想显示两行,请打开下面两行代码,(这两行代码必须在“[self addTableHeaderView]”之后)
110+
// self.showLineSwitch.on = YES;
111+
// [self showLineSwitchClicked:self.showLineSwitch];
111112
}
112113

113114
-(void)viewDidAppear:(BOOL)animated {
@@ -192,7 +193,7 @@ - (void)judgeMoreBtnShow {
192193
NSArray *sumArray = [NSArray arrayWithArray:widthArray];
193194
NSNumber* sum = [sumArray valueForKeyPath: @"@sum.self"];
194195

195-
if ([sum intValue] <= contentViewWidth) {
196+
if ([sum floatValue] <= contentViewWidth) {
196197
firstLineCellCount++;
197198
}
198199
}];
@@ -203,7 +204,7 @@ - (void)judgeMoreBtnShow {
203204
}
204205
}];
205206
[firstLineWidthArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
206-
if ([obj intValue] > contentViewWidth) {
207+
if ([obj floatValue] > contentViewWidth) {
207208
[self.collectionHeaderMoreBtnHideBoolArray replaceObjectAtIndex:idx withObject:@NO];
208209
} else {
209210
[self.collectionHeaderMoreBtnHideBoolArray replaceObjectAtIndex:idx withObject:@YES]; }
@@ -229,7 +230,7 @@ - (void)judgeMoreBtnShowWhenShowTwoRows {
229230
[widthArray addObject:@(cellWidthAndRightMargin)];
230231
NSMutableArray *sumArray = [NSMutableArray arrayWithArray:widthArray];
231232
NSNumber* sum = [sumArray valueForKeyPath: @"@sum.self"];
232-
if ([sum intValue] <= contentViewWidth) {
233+
if ([sum floatValue] <= contentViewWidth) {
233234
//未超过一行
234235
} else {
235236
//超过一行时
@@ -259,7 +260,7 @@ - (void)judgeMoreBtnShowWhenShowTwoRows {
259260
}
260261
}];
261262
[twoRowsWidthArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
262-
if ([obj intValue] > contentViewWidth) {
263+
if ([obj floatValue] > contentViewWidth) {
263264
[self.collectionHeaderMoreBtnHideBoolArray replaceObjectAtIndex:idx withObject:@NO];
264265
} else {
265266
[self.collectionHeaderMoreBtnHideBoolArray replaceObjectAtIndex:idx withObject:@YES];
@@ -302,7 +303,9 @@ - (NSUInteger)firstLineCellCountWithArray:(NSArray *)array {
302303
[widthArray addObject:@(cellWidthAndRightMargin)];
303304
NSArray *sumArray = [NSArray arrayWithArray:widthArray];
304305
NSNumber *sum = [sumArray valueForKeyPath:@"@sum.self"];
305-
if (([sum intValue] < contentViewWidth) || ([sum intValue] == contentViewWidth)) {
306+
//之所以要减去kCollectionViewToRightMargin,是为防止这种情况发生:https://i.imgur.com/6yFPQ8U.gif
307+
CGFloat firstRowWidth = [sum floatValue] - kCollectionViewToRightMargin;
308+
if ((firstRowWidth <= contentViewWidth)) {
306309
firstLineCellCount++;
307310
}
308311
}
@@ -358,6 +361,7 @@ - (UIView *)addTableHeaderView
358361
25, 30, 20);
359362
[tableHeaderView addSubview:showLineSwitch];
360363
[showLineSwitch addTarget:self action:@selector(showLineSwitchClicked:) forControlEvents:UIControlEventAllEvents];
364+
self.showLineSwitch = showLineSwitch;
361365
UILabel *subtitleLabel = [[UILabel alloc] init];
362366
subtitleLabel.frame = CGRectMake(titleLabel.frame.origin.x,
363367
CGRectGetMaxY(titleLabel.frame) + 10,

CollectionViewClassifyMenu/Class/Other/data.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,18 @@
120120
{
121121
"Food_Name": "东北菜",
122122
"Picture":"Picture"
123+
},
124+
{
125+
"Food_Name": "河南菜",
126+
"Picture":"Picture"
127+
},
128+
{
129+
"Food_Name": "四川菜",
130+
"Picture":"Picture"
131+
},
132+
{
133+
"Food_Name": "台湾菜",
134+
"Picture":"Picture"
123135
}
124136
]
125137
},

0 commit comments

Comments
 (0)