Skip to content

Commit ce86ea2

Browse files
_lemon_lemon
authored andcommitted
修改了一下算法,把比较繁琐的method删除了。
1 parent e1565c0 commit ce86ea2

File tree

1 file changed

+21
-67
lines changed

1 file changed

+21
-67
lines changed

CollectionViewClassifyMenu/Class/ClassifyMenu/Controller/CYLClassifyMenuViewController.m

Lines changed: 21 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -173,52 +173,6 @@ - (float)cellLimitWidth:(float)cellWidth
173173
return cellWidth;
174174
}
175175

176-
/*
177-
178-
@attention 特别注意:本方法已经对每个 section 的最后一个元素进行了考虑,不会加上右边的间隔。
179-
分行规则:
180-
181-
1. cell与cell之间必须有大小为kCollectionViewCellsHorizonMargin的间距,
182-
2. 左右可以没有间距。
183-
184-
那么有这样的规律:
185-
186-
1. 一旦cell+kCollectionViewCellsHorizonMargin超过contentViewWidth,则肯定要分行。
187-
2. cell超过contentViewWidth也会分行。
188-
189-
两者的区别在于cell的宽度,前者还是自身宽度,但后者已经变成了contentViewWidth的宽度。
190-
*/
191-
- (float)textImageWidthAndRightMargin:(NSString *)text
192-
content:(id)obj
193-
array:(NSArray *)array {
194-
CGFloat contentViewWidth = CGRectGetWidth(self.collectionView.frame) - kCollectionViewToLeftMargin - kCollectionViewToRightMargin;
195-
__block float cellWidth = [self collectionCellWidthText:text content:obj];
196-
__block float cellWidthAndRightMargin;
197-
if (cellWidth == contentViewWidth) {
198-
cellWidthAndRightMargin = contentViewWidth;
199-
} else {
200-
if (obj == [array lastObject]) {
201-
cellWidthAndRightMargin = cellWidth;
202-
} else {
203-
[self cellLimitWidth:cellWidth
204-
limitMargin:kCollectionViewCellsHorizonMargin
205-
isLimitWidth:^(BOOL isLimitWidth, NSNumber *data) {
206-
if (isLimitWidth) {
207-
//当cell和kCollectionViewCellsHorizonMargin总和大于contentViewWidth,
208-
//但是cell却小于contentViewWidth时,还是占一行。
209-
cellWidthAndRightMargin = contentViewWidth;
210-
} else {
211-
//这个地方只是大概的估计下,他不能判断出当加上下一个cell的cellWidthAndRightMargin大于contentViewWidth时,
212-
//cellWidthAndRightMargin右侧剩余的部分
213-
//所以必须在后续判断与下个一cell的cellWidthAndRightMargin的和超出contentViewWidth时的情况
214-
cellWidthAndRightMargin = cellWidth + kCollectionViewCellsHorizonMargin;
215-
}
216-
}];
217-
}
218-
}
219-
return cellWidthAndRightMargin;
220-
}
221-
222176
- (void)judgeMoreButtonShowWhenDefaultRowsCount:(NSUInteger)defaultRowsCount {
223177
[self.rowsCountPerSection enumerateObjectsUsingBlock:^(id __nonnull obj, NSUInteger idx, BOOL * __nonnull stop) {
224178
if ([obj integerValue] > defaultRowsCount) {
@@ -245,29 +199,29 @@ - (void)judgeMoreButtonShowWhenDefaultRowsCount:(NSUInteger)defaultRowsCount {
245199

246200
- (NSUInteger)firstRowCellCountWithArray:(NSArray *)array {
247201
CGFloat contentViewWidth = CGRectGetWidth(self.collectionView.frame) - kCollectionViewToLeftMargin - kCollectionViewToRightMargin;
248-
__block NSUInteger firstRowCellCount = 0;
249-
NSMutableArray *widthArray = [NSMutableArray array];
250-
[array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
251-
NSString *text = [obj objectForKey:kDataSourceCellTextKey];
252-
float cellWidthAndRightMargin = [self textImageWidthAndRightMargin:text
253-
content:obj
254-
array:array];
255-
[widthArray addObject:@(cellWidthAndRightMargin)];
256-
NSArray *sumArray = [NSArray arrayWithArray:widthArray];
257-
NSNumber *sum = [sumArray valueForKeyPath:@"@sum.self"];
258-
CGFloat firstRowWidth;
259-
if (obj == [array lastObject]) {
260-
firstRowWidth = [sum floatValue];
202+
NSUInteger firstRowCellCount = 0;
203+
float currentCellWidthSum = 0;
204+
float currentCellSpace = 0;
205+
for (int i = 0; i < array.count; i++) {
206+
NSString *text = [array[i] objectForKey:kDataSourceCellTextKey];
207+
float cellWidth = [self collectionCellWidthText:text content:array[i]];
208+
if (cellWidth >= contentViewWidth) {
209+
return i == 0? 1 : firstRowCellCount;
261210
} else {
262-
//之所以要减去kCollectionViewToRightMargin,是为防止这种情况发生:
263-
//⓵https://i.imgur.com/6yFPQ8U.gif ⓶https://i.imgur.com/XzfNVda.png
264-
firstRowWidth = [sum floatValue] - kCollectionViewToRightMargin;
265-
}
266-
if ((firstRowWidth <= contentViewWidth)) {
267-
firstRowCellCount++;
211+
currentCellWidthSum += cellWidth;
212+
if (i == 0) {
213+
firstRowCellCount++;
214+
continue;
215+
}
216+
currentCellSpace = (contentViewWidth - currentCellWidthSum) / firstRowCellCount;
217+
if (currentCellSpace <= kCollectionViewCellsHorizonMargin) {
218+
return firstRowCellCount;
219+
} else {
220+
firstRowCellCount++;
221+
}
268222
}
269-
}];
270-
return firstRowCellCount;
223+
}
224+
return firstRowCellCount;
271225
}
272226

273227
- (NSMutableArray *)cellsInPerRowWhenLayoutWithArray:(NSMutableArray *)array {

0 commit comments

Comments
 (0)