1313
1414@interface FMLayoutDynamicSection ()
1515
16+ @property (nonatomic , strong )NSMapTable *autoHeightCells;
17+
1618@end
1719
1820@implementation FMLayoutDynamicSection
1921
22+ - (NSMapTable *)autoHeightCells {
23+ if (_autoHeightCells == nil ) {
24+ _autoHeightCells = [NSMapTable mapTableWithKeyOptions: NSPointerFunctionsWeakMemory valueOptions: NSPointerFunctionsStrongMemory];
25+ }
26+ return _autoHeightCells;
27+ }
28+
29+ - (CGFloat)autoHeightVerticalWithWidth : (CGFloat)fixedWidth index : (NSInteger )index {
30+ CGFloat itemOther = 0 ;
31+ if (self.direction == FMLayoutDirectionHorizontal) {
32+ @throw [NSException exceptionWithName: @" autoHeightFixedWidth must for FMLayoutDirectionVertical" reason: @" FMLayoutDynamicSection" userInfo: nil ];
33+ }
34+ if (self.deqCellReturnElement ) {
35+ FMLayoutElement *element = self.deqCellReturnElement (self, index);
36+ UICollectionViewCell *cell = [self .autoHeightCells objectForKey: element.viewClass];
37+ if (cell == nil ) {
38+ if (element.isNib ) {
39+ cell = [[[NSBundle mainBundle ] loadNibNamed: NSStringFromClass (element.viewClass) owner: nil options: nil ] lastObject ];
40+ } else {
41+ cell = [[element.viewClass alloc ] init ];
42+ }
43+ [self .autoHeightCells setObject: cell forKey: element.viewClass];
44+ }
45+ if (self.configurationCell ) {
46+ self.configurationCell (self ,cell, index);
47+ }
48+ itemOther = [cell systemLayoutSizeFittingSize: CGSizeMake (fixedWidth, MAXFLOAT)].height ;
49+ }
50+ return itemOther;
51+ }
52+
2053- (id )copyWithZone : (NSZone *)zone {
2154 FMLayoutDynamicSection *section = [super copyWithZone: zone];
2255 section.autoHeightFixedWidth = self.autoHeightFixedWidth ;
@@ -64,22 +97,7 @@ - (FMCollectionLayoutAttributes *)getItemAttributesWithIndex:(NSInteger)j{
6497 CGFloat itemFixed = self.cellFixedSize ;
6598 CGFloat itemOther = 0 ;
6699 if (self.autoHeightFixedWidth ) {
67- if (self.direction == FMLayoutDirectionHorizontal) {
68- @throw [NSException exceptionWithName: @" autoHeightFixedWidth must for FMLayoutDirectionVertical" reason: @" FMLayoutDynamicSection" userInfo: nil ];
69- }
70- if (self.deqCellReturnElement ) {
71- UICollectionViewCell *cell;
72- FMLayoutElement *element = self.deqCellReturnElement (self, j);
73- if (element.isNib ) {
74- cell = [[[NSBundle mainBundle ] loadNibNamed: NSStringFromClass (element.viewClass) owner: nil options: nil ] lastObject ];
75- } else {
76- cell = [[element.viewClass alloc ] init ];
77- }
78- if (self.configurationCell ) {
79- self.configurationCell (self ,cell, j);
80- }
81- itemOther = [cell systemLayoutSizeFittingSize: CGSizeMake (itemFixed, MAXFLOAT)].height ;
82- }
100+ itemOther = [self autoHeightVerticalWithWidth: itemFixed index: j];
83101 } else {
84102 itemOther = !self.otherBlock ?0 :self.otherBlock (self, j);
85103 }
@@ -106,8 +124,6 @@ - (FMCollectionLayoutAttributes *)getItemAttributesWithIndex:(NSInteger)j{
106124 return itemAttr;
107125}
108126
109-
110-
111127- (UICollectionViewCell *)dequeueReusableCellForIndexPath : (NSIndexPath *)indexPath collectionView : (nonnull UICollectionView *)collectionView {
112128 return [collectionView dequeueReusableCellWithReuseIdentifier: self .deqCellReturnElement (self , indexPath.item).reuseIdentifier forIndexPath: indexPath];
113129}
0 commit comments