1+ // MIT License
2+ //
13// Copyright (c) ( https://github.com/liangdahong )
4+ //
5+ // Permission is hereby granted, free of charge, to any person obtaining a copy
6+ // of this software and associated documentation files (the "Software"), to deal
7+ // in the Software without restriction, including without limitation the rights
8+ // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+ // copies of the Software, and to permit persons to whom the Software is
10+ // furnished to do so, subject to the following conditions:
11+ //
12+ // The above copyright notice and this permission notice shall be included in all
13+ // copies or substantial portions of the Software.
14+ //
15+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+ // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+ // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+ // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+ // SOFTWARE.
222
323#import " UITableView+BMTemplateLayoutCell.h"
424#import < objc/runtime.h>
@@ -127,6 +147,11 @@ - (UIView *)bm_tempViewCellWithCellClass:(Class)clas {
127147}
128148
129149- (CGFloat)bm_layoutIfNeededCellWith : (UITableViewCell *)cell configuration : (BMLayoutCellConfigurationBlock)configuration {
150+
151+ if (self.superview ) {
152+ [self .superview layoutIfNeeded ];
153+ }
154+
130155 cell.superview .frame = CGRectMake (0 .0f , 0 .0f , self.frame .size .width , 0 .0f );
131156 cell.frame = CGRectMake (0 .0f , 0 .0f , self.frame .size .width , 0 .0f );
132157 configuration (cell);
@@ -433,15 +458,16 @@ - (UIView *)bm_tempViewHeaderFooterViewWithHeaderFooterViewClass:(Class)clas {
433458 }
434459 UIView *tempView = objc_getAssociatedObject (self, (__bridge const void *)(noReuseIdentifierChar));
435460 if (!tempView) {
436-
461+ // 没有绑定就创建
437462 NSString *path = [[NSBundle mainBundle ] pathForResource: NSStringFromClass (clas.class) ofType: @" nib" ];
438463 UITableViewHeaderFooterView *noCachetableViewHeaderFooterView = nil ;
439464 if (path.length ) {
440465 noCachetableViewHeaderFooterView = [[NSBundle mainBundle ] loadNibNamed: NSStringFromClass (clas) owner: nil options: nil ][0 ];
441466 } else {
442467 noCachetableViewHeaderFooterView = [[clas alloc ] initWithReuseIdentifier: noReuseIdentifier];
443468 }
444-
469+
470+ // 绑定起来
445471 tempView = UIView.new ;
446472 [tempView addSubview: noCachetableViewHeaderFooterView];
447473 objc_setAssociatedObject (self, (__bridge const void *)(noReuseIdentifierChar), tempView, OBJC_ASSOCIATION_RETAIN_NONATOMIC );
@@ -451,19 +477,55 @@ - (UIView *)bm_tempViewHeaderFooterViewWithHeaderFooterViewClass:(Class)clas {
451477}
452478
453479- (CGFloat)bm_layoutIfNeededHeaderFooterViewWith : (UITableViewHeaderFooterView *)tableViewHeaderFooterView configuration : (BMLayoutHeaderFooterViewConfigurationBlock)configuration {
480+
481+ if (self.superview ) {
482+ [self .superview layoutIfNeeded ];
483+ }
484+
454485 tableViewHeaderFooterView.superview .frame = CGRectMake (0 , 0 , self.frame .size .width , 0 );
455486 tableViewHeaderFooterView.frame = CGRectMake (0 , 0 , self.frame .size .width , 0 );
456487 configuration (tableViewHeaderFooterView);
457488 [tableViewHeaderFooterView.superview layoutIfNeeded ];
458- CGFloat maxY = 0 .0f ;
459- bm_templateLayout_get_view_subviews_MaxY (tableViewHeaderFooterView, &maxY, nil );
460- return maxY;
489+ if (!tableViewHeaderFooterView.isDynamicHeaderFooterBottomView ) {
490+ // cell 的最底部View是固定的
491+ if (tableViewHeaderFooterView.linView ) {
492+ // 如果有关联就可直接取最底部View的MaxY为Cell的高度
493+ return CGRectGetMaxY (tableViewHeaderFooterView.linView .frame );
494+ } else {
495+ // 还没有关联就遍历获取 MaxY,同时关联
496+ CGFloat maxY = 0 .0f ;
497+ UIView *v = nil ;
498+ bm_templateLayout_get_view_subviews_MaxY (tableViewHeaderFooterView, &maxY, &v);
499+ tableViewHeaderFooterView.linView = v;
500+ return maxY;
501+ }
502+ } else {
503+ CGFloat maxY = 0 .0f ;
504+ bm_templateLayout_get_view_subviews_MaxY (tableViewHeaderFooterView, &maxY, nil );
505+ return maxY;
506+ }
461507}
462508
463509@end
464510
465511@implementation UITableViewCell (BMTemplateLayoutCell)
466512
513+ - (BOOL )isDynamicCellBottomView {
514+ return [objc_getAssociatedObject (self , _cmd ) boolValue ];
515+ }
516+
517+ - (void )setDynamicCellBottomView : (BOOL )dynamicCellBottomView {
518+ objc_setAssociatedObject (self, @selector (isDynamicCellBottomView ), @(dynamicCellBottomView), OBJC_ASSOCIATION_RETAIN_NONATOMIC );
519+ }
520+
521+ - (UIView *)linView {
522+ return objc_getAssociatedObject (self, _cmd);
523+ }
524+
525+ - (void )setLinView : (UIView *)linView {
526+ objc_setAssociatedObject (self, @selector (linView ), linView, OBJC_ASSOCIATION_RETAIN_NONATOMIC );
527+ }
528+
467529+ (instancetype )bm_tableViewCellWithTableView : (UITableView *)tableView {
468530 NSString *selfClassName = NSStringFromClass (self.class );
469531 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: selfClassName];
@@ -480,12 +542,18 @@ + (instancetype)bm_tableViewCellWithTableView:(UITableView *)tableView {
480542 return cell;
481543}
482544
483- - (BOOL )isDynamicCellBottomView {
545+ @end
546+
547+ #pragma mark - UITableViewHeaderFooterView BMTemplateLayoutCell
548+
549+ @implementation UITableViewHeaderFooterView (BMTemplateLayoutCell)
550+
551+ - (BOOL )isDynamicHeaderFooterBottomView {
484552 return [objc_getAssociatedObject (self , _cmd ) boolValue ];
485553}
486554
487- - (void )setDynamicCellBottomView : (BOOL )dynamicCellBottomView {
488- objc_setAssociatedObject (self, @selector (isDynamicCellBottomView ), @(dynamicCellBottomView ), OBJC_ASSOCIATION_RETAIN_NONATOMIC );
555+ - (void )setDynamicHeaderFooterBottomView : (BOOL )dynamicHeaderFooterBottomView {
556+ objc_setAssociatedObject (self, @selector (isDynamicHeaderFooterBottomView ), @(dynamicHeaderFooterBottomView ), OBJC_ASSOCIATION_RETAIN_NONATOMIC );
489557}
490558
491559- (UIView *)linView {
@@ -496,10 +564,6 @@ - (void)setLinView:(UIView *)linView {
496564 objc_setAssociatedObject (self, @selector (linView ), linView, OBJC_ASSOCIATION_RETAIN_NONATOMIC );
497565}
498566
499- @end
500-
501- @implementation UITableViewHeaderFooterView (BMTemplateLayoutCell)
502-
503567+ (instancetype )bm_tableViewHeaderFooterViewWithTableView : (UITableView *)tableView {
504568 NSString *selfClassName = NSStringFromClass (self.class );
505569 UITableViewHeaderFooterView *headerFooterView = [tableView dequeueReusableHeaderFooterViewWithIdentifier: selfClassName];
@@ -517,4 +581,3 @@ + (instancetype)bm_tableViewHeaderFooterViewWithTableView:(UITableView *)tableVi
517581}
518582
519583@end
520-
0 commit comments