3030
3131static NSString * const kCellIdentifier = @" CellIdentifier" ;
3232static NSString * const kHeaderViewCellIdentifier = @" HeaderViewCellIdentifier" ;
33-
33+ typedef void (^ISLimitWidth)( BOOL yesORNo, id data);
3434@interface CYLClassifyMenuViewController ()
3535<
3636UICollectionViewDataSource,
@@ -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
0 commit comments