Skip to content

Commit 12063bd

Browse files
author
pifupro_iOS_admin
committed
【Bug Fixed】当 cell 内容的长度大于 collection view 的宽度时,相应改变moreButton 的显示。
1 parent 1d18a11 commit 12063bd

File tree

3 files changed

+68
-37
lines changed

3 files changed

+68
-37
lines changed

CollectionViewClassifyMenu.xcodeproj/xcuserdata/chenyilong.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@
4444
ignoreCount = "0"
4545
continueAfterRunningActions = "No"
4646
filePath = "CollectionViewClassifyMenu/Class/Controller/CYLClassifyMenuViewController.m"
47-
timestampString = "451332831.160988"
47+
timestampString = "451338277.028937"
4848
startingColumnNumber = "9223372036854775807"
4949
endingColumnNumber = "9223372036854775807"
50-
startingLineNumber = "451"
51-
endingLineNumber = "451"
50+
startingLineNumber = "486"
51+
endingLineNumber = "486"
5252
landmarkName = "-filterHeaderViewMoreBtnClicked:"
5353
landmarkType = "5">
5454
</BreakpointContent>

CollectionViewClassifyMenu/Class/Controller/CYLClassifyMenuViewController.m

Lines changed: 61 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
static NSString * const kCellIdentifier = @"CellIdentifier";
3232
static NSString * const kHeaderViewCellIdentifier = @"HeaderViewCellIdentifier";
33-
33+
typedef void(^ISLimitWidth)(BOOL yesORNo,id data);
3434
@interface CYLClassifyMenuViewController ()
3535
<
3636
UICollectionViewDataSource,
@@ -120,6 +120,38 @@ - (void)initData {
120120
self.dataSource = [NSMutableArray arrayWithArray:json];
121121
}
122122

123+
- (float)checkCellLimitWidth:(float)cellWidth isLimitWidth:(ISLimitWidth)isLimitWidth {
124+
float limitWidth = (CGRectGetWidth(self.collectionView.frame)-kCollectionViewToLeftMargin-kCollectionViewToRightMargin);
125+
if (cellWidth >= limitWidth) {
126+
cellWidth = limitWidth;
127+
isLimitWidth?isLimitWidth(YES,@(cellWidth)):nil;
128+
return cellWidth;
129+
}
130+
isLimitWidth?isLimitWidth(NO,@(cellWidth)):nil;
131+
return cellWidth;
132+
}
133+
- (float)getTextAndImageWidth:(NSString *)text content:(id)obj array:(NSArray *)array {
134+
__block float cellWidth = [self getCollectionCellWidthText:text content:obj];
135+
__block float cellWidthAndRightMargin;
136+
137+
[self checkCellLimitWidth:cellWidth isLimitWidth:^(BOOL yesORNo, NSNumber *data) {
138+
cellWidth = [data floatValue];
139+
if (yesORNo == YES) {
140+
cellWidthAndRightMargin = (CGRectGetWidth(self.collectionView.frame)-kCollectionViewToLeftMargin-kCollectionViewToRightMargin);
141+
} else {
142+
if (((CGRectGetWidth(self.collectionView.frame)-kCollectionViewToLeftMargin-kCollectionViewToRightMargin)-cellWidth >=kCollectionViewCellsHorizonMargin)) {
143+
if (obj == [array lastObject]) {
144+
cellWidthAndRightMargin = cellWidth;
145+
} else {
146+
cellWidthAndRightMargin = cellWidth+kCollectionViewCellsHorizonMargin;
147+
}
148+
} else {
149+
cellWidthAndRightMargin = (CGRectGetWidth(self.collectionView.frame)-kCollectionViewToLeftMargin-kCollectionViewToRightMargin);
150+
}
151+
}
152+
}];
153+
return cellWidthAndRightMargin;
154+
}
123155
- (void)judgeMoreBtnShow {
124156
NSMutableArray *firstLineWidthArray = [NSMutableArray array];
125157
__weak __typeof(self) weakSelf = self;
@@ -132,14 +164,8 @@ - (void)judgeMoreBtnShow {
132164
__weak __typeof(symptoms) weakSymptoms = symptoms;
133165
[symptoms enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
134166
NSString *text = [obj objectForKey:kDataSourceCellTextKey];
135-
float cellWidth = [strongSelf getCollectionCellWidthText:text content:obj];
136-
float textAndImageWidth;
137-
if (obj == [weakSymptoms lastObject]) {
138-
textAndImageWidth = cellWidth;
139-
} else {
140-
textAndImageWidth = cellWidth+kCollectionViewCellsHorizonMargin;
141-
}
142-
[widthArray addObject:@(textAndImageWidth)];
167+
float cellWidthAndRightMargin = [strongSelf getTextAndImageWidth:text content:obj array:weakSymptoms];
168+
[widthArray addObject:@(cellWidthAndRightMargin)];
143169
NSArray *sumArray = [NSArray arrayWithArray:widthArray];
144170
NSNumber* sum = [sumArray valueForKeyPath: @"@sum.self"];
145171
if ([sum intValue]<(strongSelf.collectionView.frame.size.width-kCollectionViewToLeftMargin-kCollectionViewToRightMargin)||[sum intValue]==(strongSelf.collectionView.frame.size.width-kCollectionViewToLeftMargin-kCollectionViewToRightMargin)) {
@@ -176,14 +202,8 @@ - (void)judgeMoreBtnShowWhenShowTwoLine {
176202
__weak __typeof(symptoms) weakSymptoms = symptoms;
177203
[symptoms enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
178204
NSString *text = [obj objectForKey:kDataSourceCellTextKey];
179-
float cellWidth = [strongSelf getCollectionCellWidthText:text content:obj];
180-
float textAndImageWidth;
181-
if (obj == [weakSymptoms lastObject]) {
182-
textAndImageWidth = cellWidth;
183-
} else {
184-
textAndImageWidth = cellWidth+kCollectionViewCellsHorizonMargin;
185-
}
186-
[widthArray addObject:@(textAndImageWidth)];
205+
float cellWidthAndRightMargin = [strongSelf getTextAndImageWidth:text content:obj array:weakSymptoms];
206+
[widthArray addObject:@(cellWidthAndRightMargin)];
187207
NSMutableArray *sumArray = [NSMutableArray arrayWithArray:widthArray];
188208
NSNumber* sum = [sumArray valueForKeyPath: @"@sum.self"];
189209
if ([sum intValue]<(strongSelf.collectionView.frame.size.width-kCollectionViewToLeftMargin-kCollectionViewToRightMargin)||[sum intValue]==(strongSelf.collectionView.frame.size.width-kCollectionViewToLeftMargin-kCollectionViewToRightMargin)) {
@@ -192,12 +212,25 @@ - (void)judgeMoreBtnShowWhenShowTwoLine {
192212
//超过一行时
193213
if(countTime==0) {
194214
[widthArray removeAllObjects];
195-
if (obj == [weakSymptoms lastObject]) {
196-
[widthArray addObject:@(textAndImageWidth+kCollectionViewCellsHorizonMargin)];
197-
} else {
198-
[widthArray addObject:@(textAndImageWidth)];
199-
}
200-
}
215+
[self checkCellLimitWidth:cellWidthAndRightMargin isLimitWidth:^(BOOL yesORNo, NSNumber *data) {
216+
if (yesORNo == YES) {
217+
[widthArray addObject:@((CGRectGetWidth(self.collectionView.frame)-kCollectionViewToLeftMargin-kCollectionViewToRightMargin))];
218+
} else {
219+
if (((CGRectGetWidth(self.collectionView.frame)-kCollectionViewToLeftMargin-kCollectionViewToRightMargin)-cellWidthAndRightMargin >=kCollectionViewCellsHorizonMargin)) {
220+
if (obj == [weakSymptoms lastObject]) {
221+
[widthArray addObject:@(cellWidthAndRightMargin+kCollectionViewCellsHorizonMargin)];
222+
} else {
223+
[widthArray addObject:@((CGRectGetWidth(self.collectionView.frame)-kCollectionViewToLeftMargin-kCollectionViewToRightMargin))];
224+
}
225+
226+
} else {
227+
[widthArray addObject:@((CGRectGetWidth(self.collectionView.frame)-kCollectionViewToLeftMargin-kCollectionViewToRightMargin))];
228+
229+
}
230+
}
231+
}];
232+
233+
}
201234
countTime++;
202235
}
203236
}];
@@ -248,13 +281,13 @@ - (int)getFirstLineCellCountWithArray:(NSArray *)array {
248281
@autoreleasepool {
249282
NSString *text = [obj objectForKey:kDataSourceCellTextKey];
250283
float cellWidth = [self getCollectionCellWidthText:text content:obj];
251-
float textAndImageWidth;
284+
float cellWidthAndRightMargin;
252285
if (obj == [weakArray lastObject]) {
253-
textAndImageWidth = cellWidth;
286+
cellWidthAndRightMargin = cellWidth;
254287
} else {
255-
textAndImageWidth = cellWidth+kCollectionViewCellsHorizonMargin;
288+
cellWidthAndRightMargin = cellWidth+kCollectionViewCellsHorizonMargin;
256289
}
257-
[widthArray addObject:@(textAndImageWidth)];
290+
[widthArray addObject:@(cellWidthAndRightMargin)];
258291
NSArray *sumArray = [NSArray arrayWithArray:widthArray];
259292
NSNumber* sum = [sumArray valueForKeyPath: @"@sum.self"];
260293
if ([sum intValue]<(self.collectionView.frame.size.width-kCollectionViewToLeftMargin-kCollectionViewToRightMargin)||[sum intValue]==(self.collectionView.frame.size.width-kCollectionViewToLeftMargin-kCollectionViewToRightMargin)) {
@@ -287,9 +320,7 @@ - (float)getCollectionCellWidthText:(NSString *)text content:(NSDictionary *)con
287320
cellWidth = ceilf(size.width) + kCellBtnCenterToBorderMargin;
288321
}
289322

290-
if (cellWidth >= (CGRectGetWidth(self.collectionView.frame)- kCollectionViewToRightMargin *2)) {
291-
cellWidth = CGRectGetWidth(self.collectionView.frame)- kCollectionViewToRightMargin *2;
292-
}
323+
cellWidth = [self checkCellLimitWidth:cellWidth isLimitWidth:nil];
293324
return cellWidth;
294325
}
295326

CollectionViewClassifyMenu/Class/Model/data.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
"Food_Name": "焖面",
5454
},
5555
{
56-
"Food_Name": "凉面",
56+
"Food_Name": "凉面凉面凉面凉面凉面凉面凉面凉面凉面凉面凉面凉面凉面",
5757
}
5858
]
5959
},
@@ -100,7 +100,7 @@
100100
"Food_Name": "奶油蛋糕",
101101
},
102102
{
103-
"Food_Name": "磅蛋糕",
103+
"Food_Name": "磅蛋糕磅蛋糕磅蛋糕磅蛋糕磅蛋糕磅蛋糕磅蛋糕磅蛋糕磅蛋糕磅蛋糕",
104104
}
105105
]
106106
},
@@ -118,7 +118,7 @@
118118
"Picture":"Picture"
119119
},
120120
{
121-
"Food_Name": "东北菜",
121+
"Food_Name": "东北菜东北菜东北菜东北菜东北菜东北菜东北菜东北菜",
122122
"Picture":"Picture"
123123
}
124124
]
@@ -139,7 +139,7 @@
139139
"Picture":"Picture"
140140
},
141141
{
142-
"Food_Name": "豆浆机食谱",
142+
"Food_Name": "豆浆机食谱豆浆机食谱豆浆机食谱豆浆机食谱豆浆机食谱豆浆机食谱",
143143
"Picture":"Picture"
144144
}
145145
]

0 commit comments

Comments
 (0)