iOSiOS开发iOS开发资料收集区

UITableView+FDTemplateLayoutCell

2016-11-27  本文已影响1735人  shawenlx

UITableView+FDTemplateLayoutCell 源码探究



代码文件目录

- UITableView+FDIndexPathHeightCache.h
- UITableView+FDIndexPathHeightCache.m
- UITableView+FDKeyedHeightCache.h
- UITableView+FDKeyedHeightCache.m
- UITableView+FDTemplateLayoutCell.h
- UITableView+FDTemplateLayoutCell.m
- UITableView+FDTemplateLayoutCellDebug.h
- UITableView+FDTemplateLayoutCellDebug.m

首先,介绍一下这几个类的基本功能,再层层推进,逐一分析。

- UITableView+FDIndexPathHeightCache,主要负责cell通过NSIndexPath进行缓存高度的功能
- UITableView+FDKeyedHeightCache,主要负责cell通过key值进行缓存高度的功能
- UITableView+FDTemplateLayoutCell,提供接口方法方便用户定义cell的数据源,以及帮助我们计算cell的高度
- UITableView+FDTemplateLayoutCellDebug,提供一些Debug打印信息

关于这个框架,坦白说,从代码中看,作者无疑秀了一波runtime底层的功底,让我这种小白起初一脸懵逼。自然我得换种思路来解读这个框架,那就是从字数最少的类入手吧。

UITableView+FDTemplateLayoutCellDebug

@interface UITableView (FDTemplateLayoutCellDebug)

//设置Debug模式是否打开
@property (nonatomic, assign) BOOL fd_debugLogEnabled;

//通过该方法,传递NSLog打印对应的Debug信息
- (void)fd_debugLog:(NSString *)message;

@end
@implementation UITableView (FDTemplateLayoutCellDebug)

- (BOOL)fd_debugLogEnabled {
    return [objc_getAssociatedObject(self, _cmd) boolValue];
}

- (void)setFd_debugLogEnabled:(BOOL)debugLogEnabled {
    objc_setAssociatedObject(self, @selector(fd_debugLogEnabled), @(debugLogEnabled), OBJC_ASSOCIATION_RETAIN);
}

- (void)fd_debugLog:(NSString *)message {
    if (self.fd_debugLogEnabled) {
        NSLog(@"** FDTemplateLayoutCell ** %@", message);
    }
}

@end

UITableView+FDKeyedHeightCache

#import <UIKit/UIKit.h>

@interface FDKeyedHeightCache : NSObject

//判断缓存中是否存在key为值的缓存高度
- (BOOL)existsHeightForKey:(id<NSCopying>)key;

//对指定key的cell设置高度为height
- (void)cacheHeight:(CGFloat)height byKey:(id<NSCopying>)key;

//从缓存中获取对应key的cell的高度height值
- (CGFloat)heightForKey:(id<NSCopying>)key;

//从缓存中删除指定key的cell的值
- (void)invalidateHeightForKey:(id<NSCopying>)key;

//移除缓存中所有的cell的高度缓存值
- (void)invalidateAllHeightCache;
@end

@interface UITableView (FDKeyedHeightCache)
@property (nonatomic, strong, readonly) FDKeyedHeightCache *fd_keyedHeightCache;
@end
@property (nonatomic, strong) NSMutableDictionary<id<NSCopying>, NSNumber *> *mutableHeightsByKeyForPortrait;

@property (nonatomic, strong) NSMutableDictionary<id<NSCopying>, NSNumber *> *mutableHeightsByKeyForLandscape;

不难看出,这是两个指定泛型的可变字典。


- (BOOL)existsHeightForKey:(id<NSCopying>)key {
    NSNumber *number = self.mutableHeightsByKeyForCurrentOrientation[key];
    return number && ![number isEqualToNumber:@-1];
}

- (void)cacheHeight:(CGFloat)height byKey:(id<NSCopying>)key {
    self.mutableHeightsByKeyForCurrentOrientation[key] = @(height);
}

- (CGFloat)heightForKey:(id<NSCopying>)key {
#if CGFLOAT_IS_DOUBLE
    return [self.mutableHeightsByKeyForCurrentOrientation[key] doubleValue];
#else
    return [self.mutableHeightsByKeyForCurrentOrientation[key] floatValue];
#endif
}

- (void)invalidateHeightForKey:(id<NSCopying>)key {
    [self.mutableHeightsByKeyForPortrait removeObjectForKey:key];
    [self.mutableHeightsByKeyForLandscape removeObjectForKey:key];
}

- (void)invalidateAllHeightCache {
    [self.mutableHeightsByKeyForPortrait removeAllObjects];
    [self.mutableHeightsByKeyForLandscape removeAllObjects];
}
- (NSMutableDictionary<id<NSCopying>, NSNumber *> *)mutableHeightsByKeyForCurrentOrientation {
    return UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation) ? self.mutableHeightsByKeyForPortrait: self.mutableHeightsByKeyForLandscape;
}

UITableView+FDIndexPathHeightCache

@interface FDIndexPathHeightCache : NSObject

// 如果您使用索引路径获取高度缓存,则自动启用
@property (nonatomic, assign) BOOL automaticallyInvalidateEnabled;

// Height cache
- (BOOL)existsHeightAtIndexPath:(NSIndexPath *)indexPath;
- (void)cacheHeight:(CGFloat)height byIndexPath:(NSIndexPath *)indexPath;
- (CGFloat)heightForIndexPath:(NSIndexPath *)indexPath;
- (void)invalidateHeightAtIndexPath:(NSIndexPath *)indexPath;
- (void)invalidateAllHeightCache;

@end

@interface UITableView (FDIndexPathHeightCache)
@property (nonatomic, strong, readonly) FDIndexPathHeightCache *fd_indexPathHeightCache;
@end

@interface UITableView (FDIndexPathHeightCacheInvalidation)
/// 当你不想通过删除缓存中的高度来刷新数据源重新计算时,可以调用这个方法。
/// 该方法中用过runtime重写了tableView中修改cell的一些方法,例如插入cell,删除cell,移动cell,以及reloadData方法。
- (void)fd_reloadDataWithoutInvalidateIndexPathHeightCache;
@end
typedef NSMutableArray<NSMutableArray<NSNumber *> *> FDIndexPathHeightsBySection;

@interface FDIndexPathHeightCache ()
@property (nonatomic, strong) FDIndexPathHeightsBySection *heightsBySectionForPortrait;
@property (nonatomic, strong) FDIndexPathHeightsBySection *heightsBySectionForLandscape;
@end

通过前面key的高度缓存分析,不难猜出这几个属性是干什么的。


- (void)enumerateAllOrientationsUsingBlock:(void (^)(FDIndexPathHeightsBySection *heightsBySection))block {
    block(self.heightsBySectionForPortrait);
    block(self.heightsBySectionForLandscape);
}

- (void)invalidateHeightAtIndexPath:(NSIndexPath *)indexPath {
    [self buildCachesAtIndexPathsIfNeeded:@[indexPath]];
    [self enumerateAllOrientationsUsingBlock:^(FDIndexPathHeightsBySection *heightsBySection {
        heightsBySection[indexPath.section][indexPath.row] = @-1;
    }];
}

- (void)invalidateAllHeightCache {
    [self enumerateAllOrientationsUsingBlock:^(FDIndexPathHeightsBySection *heightsBySection) {
        [heightsBySection removeAllObjects];
    }];
}

- (void)buildCachesAtIndexPathsIfNeeded:(NSArray *)indexPaths {
    // Build every section array or row array which is smaller than given index path.
    [indexPaths enumerateObjectsUsingBlock:^(NSIndexPath *indexPath, NSUInteger idx, BOOL *stop) {
        [self buildSectionsIfNeeded:indexPath.section];
        [self buildRowsIfNeeded:indexPath.row inExistSection:indexPath.section];
    }];
}

- (void)buildSectionsIfNeeded:(NSInteger)targetSection {
    [self enumerateAllOrientationsUsingBlock:^(FDIndexPathHeightsBySection *heightsBySection) {
        for (NSInteger section = 0; section <= targetSection; ++section) {
            if (section >= heightsBySection.count) {
                heightsBySection[section] = [NSMutableArray array];
            }
        }
    }];
}

- (void)buildRowsIfNeeded:(NSInteger)targetRow inExistSection:(NSInteger)section {
    [self enumerateAllOrientationsUsingBlock:^(FDIndexPathHeightsBySection *heightsBySection) {
        NSMutableArray<NSNumber *> *heightsByRow = heightsBySection[section];
        for (NSInteger row = 0; row <= targetRow; ++row) {
            if (row >= heightsByRow.count) {
                heightsByRow[row] = @-1;
            }
        }
    }];
}


//我们只是转发主调用,在崩溃报告中,最顶层的方法在堆栈可能存在FD,
//但它真的不是我们的错误,你应该检查你的表视图的数据源和
//重新加载时显示单元格不匹配。
static void __FD_TEMPLATE_LAYOUT_CELL_PRIMARY_CALL_IF_CRASH_NOT_OUR_BUG__(void (^callout)(void)) {
    callout();
}
#define FDPrimaryCall(...) do {__FD_TEMPLATE_LAYOUT_CELL_PRIMARY_CALL_IF_CRASH_NOT_OUR_BUG__(^{__VA_ARGS__});} while(0)
- (void)fd_reloadDataWithoutInvalidateIndexPathHeightCache {
    FDPrimaryCall([self fd_reloadData];);
}

+ (void)load {
    // All methods that trigger height cache's invalidation
    SEL selectors[] = {
        @selector(reloadData),
        @selector(insertSections:withRowAnimation:),
        @selector(deleteSections:withRowAnimation:),
        @selector(reloadSections:withRowAnimation:),
        @selector(moveSection:toSection:),
        @selector(insertRowsAtIndexPaths:withRowAnimation:),
        @selector(deleteRowsAtIndexPaths:withRowAnimation:),
        @selector(reloadRowsAtIndexPaths:withRowAnimation:),
        @selector(moveRowAtIndexPath:toIndexPath:)
    };
    
    for (NSUInteger index = 0; index < sizeof(selectors) / sizeof(SEL); ++index) {
        SEL originalSelector = selectors[index];
        SEL swizzledSelector = NSSelectorFromString([@"fd_" stringByAppendingString:NSStringFromSelector(originalSelector)]);
        Method originalMethod = class_getInstanceMethod(self, originalSelector);
        Method swizzledMethod = class_getInstanceMethod(self, swizzledSelector);
        method_exchangeImplementations(originalMethod, swizzledMethod);
    }
}

UITableView+FDTemplateLayoutCell

至此,我们已经分析了几个子类的实现逻辑,唯一剩下一个分类,也是我们使用这个框架的入口 FDTemplateLayoutCell分类。全面了解这个组件近在咫尺。

@interface UITableView (FDTemplateLayoutCell)

/* 为给定的重用标识符访问内部模板布局单元格。
 * 一般来说,你不需要知道这些模板布局单元格。
 * @param identifier重用必须注册的单元格的标识符。
*/ 
- (__kindof UITableViewCell *)fd_templateCellForReuseIdentifier:(NSString *)identifier;

/* 返回由重用标识符指定并配置的类型的单元格的高度, 并通过block来配置。
 * 单元格将被放置在固定宽度,垂直扩展的基础上,相对于其动态内容,使用自动布局。 
 * 因此,这些必要的单元被设置为自适应,即其内容总是确定它的宽度给定的宽度等于tableview的宽度。
 * @param identifier用于检索和维护模板的字符串标识符cell通过系统方法
 * '- dequeueReusableCellWithIdentifier:'
 * @param configuration用于配置和提供内容的可选块到模板单元格。 
 * 配置应该是最小的滚动性能足以计算单元格的高度。
*/
- (CGFloat)fd_heightForCellWithIdentifier:(NSString *)identifier configuration:(void (^)(id cell))configuration;

/* 计算的高度将通过其索引路径进行高速缓存,当需要时返回高速缓存的高度,因此,可以节省大量额外的高度计算。
 * 无需担心数据源更改时使缓存高度无效,它将在调用“-reloadData”或任何触发方法时自动完成UITableView的重新加载。
 * @param indexPath此单元格的高度缓存所属的位置。
*/
- (CGFloat)fd_heightForCellWithIdentifier:(NSString *)identifier cacheByIndexPath:(NSIndexPath *)indexPath configuration:(void (^)(id cell))configuration;

/* 此方法通过模型实体的标识符缓存高度。
 * 如果你的模型改变,调用“-invalidateHeightForKey:(id <NSCopying>)key”到无效缓存并重新计算,它比“cacheByIndexPath”方便得多。
 * @param key model entity的标识符,其数据配置一个单元格。
*/ 
- (CGFloat)fd_heightForCellWithIdentifier:(NSString *)identifier cacheByKey:(id<NSCopying>)key configuration:(void (^)(id cell))configuration;

@end

@interface UITableView (FDTemplateLayoutHeaderFooterView)

/* 返回在具有重用标识符的表视图中注册的Header或Footer视图的高度。
 * 在调用“ - [UITableView registerNib / Class:forHeaderFooterViewReuseIdentifier]”之后使用它,
 * 与“-fd_heightForCellWithIdentifier:configuration:”相同。
 * 它将调用“-sizeThatFits:”
 * UITableViewHeaderFooterView的子类不使用自动布局。
*/ 
- (CGFloat)fd_heightForHeaderFooterViewWithIdentifier:(NSString *)identifier configuration:(void (^)(id headerFooterView))configuration;

@end

@interface UITableViewCell (FDTemplateLayoutCell)
/* 指示这是仅用于计算的模板布局单元格。
 * 当配置单元格时,如果有非UI的副作用,你可能需要这个。
 * 类似:
 *   - (void)configureCell:(FooCell *)cell atIndexPath:(NSIndexPath *)indexPath {
 *       cell.entity = [self entityAtIndexPath:indexPath];
 *       if (!cell.fd_isTemplateLayoutCell) {
 *           [self notifySomething]; // non-UI side effects
 *       }
 *   }
*/
@property (nonatomic, assign) BOOL fd_isTemplateLayoutCell;

/* 启用以强制此模板布局单元格使用“框架布局”而不是“自动布局”,
 * 并且通过调用“-sizeThatFits:”来询问单元格的高度,所以你必须重写这个方法。
 * 仅当要手动控制此模板布局单元格的高度时才使用此属性
 * 计算模式,默认为NO。
*/
@property (nonatomic, assign) BOOL fd_enforceFrameLayout;

@end

- (CGFloat)fd_heightForCellWithIdentifier:(NSString *)identifier cacheByIndexPath:(NSIndexPath *)indexPath configuration:(void (^)(id cell))configuration {
    if (!identifier || !indexPath) {
        return 0;
    }
    
    // Hit cache
    if ([self.fd_indexPathHeightCache existsHeightAtIndexPath:indexPath]) {
        return [self.fd_indexPathHeightCache heightForIndexPath:indexPath];
    }
    
    CGFloat height = [self fd_heightForCellWithIdentifier:identifier configuration:configuration];
    [self.fd_indexPathHeightCache cacheHeight:height byIndexPath:indexPath];

    return height;
}

- (CGFloat)fd_heightForCellWithIdentifier:(NSString *)identifier cacheByKey:(id<NSCopying>)key configuration:(void (^)(id cell))configuration {
    if (!identifier || !key) {
        return 0;
    }
    
    // Hit cache
    if ([self.fd_keyedHeightCache existsHeightForKey:key]) {
        CGFloat cachedHeight = [self.fd_keyedHeightCache heightForKey:key];
        return cachedHeight;
    }
    
    CGFloat height = [self fd_heightForCellWithIdentifier:identifier configuration:configuration];
    [self.fd_keyedHeightCache cacheHeight:height byKey:key];
    return height;
}


- (CGFloat)fd_heightForCellWithIdentifier:(NSString *)identifier configuration:(void (^)(id cell))configuration {
    if (!identifier) {
        return 0;
    }
    
    UITableViewCell *templateLayoutCell = [self fd_templateCellForReuseIdentifier:identifier];
    
    //手动调用以确保与实际单元格的一致行为。 (显示在屏幕上)
    [templateLayoutCell prepareForReuse];
    
    if (configuration) {
        configuration(templateLayoutCell);
    }
    
    return [self fd_systemFittingHeightForConfiguratedCell:templateLayoutCell];
}
- (__kindof UITableViewCell *)fd_templateCellForReuseIdentifier:(NSString *)identifier {
    NSAssert(identifier.length > 0, @"Expect a valid identifier - %@", identifier);
    
    NSMutableDictionary<NSString *, UITableViewCell *> *templateCellsByIdentifiers = objc_getAssociatedObject(self, _cmd);
    if (!templateCellsByIdentifiers) {
        templateCellsByIdentifiers = @{}.mutableCopy;
        objc_setAssociatedObject(self, _cmd, templateCellsByIdentifiers, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    
    UITableViewCell *templateCell = templateCellsByIdentifiers[identifier];
    
    if (!templateCell) {
        templateCell = [self dequeueReusableCellWithIdentifier:identifier];
        NSAssert(templateCell != nil, @"Cell must be registered to table view for identifier - %@", identifier);
        templateCell.fd_isTemplateLayoutCell = YES;
        templateCell.contentView.translatesAutoresizingMaskIntoConstraints = NO;
        templateCellsByIdentifiers[identifier] = templateCell;
        [self fd_debugLog:[NSString stringWithFormat:@"layout cell created - %@", identifier]];
    }
    
    return templateCell;
}

- (CGFloat)fd_systemFittingHeightForConfiguratedCell:(UITableViewCell *)cell {
    CGFloat contentViewWidth = CGRectGetWidth(self.frame);
    
    if (cell.accessoryView) {
        //如果有定制accessoryView,则去除这个宽度
        contentViewWidth -= 16 + CGRectGetWidth(cell.accessoryView.frame);
    } else {
        //如果有系统accessoryView展示,则去除对应的宽度。
        static const CGFloat systemAccessoryWidths[] = {
            [UITableViewCellAccessoryNone] = 0,
            [UITableViewCellAccessoryDisclosureIndicator] = 34,
            [UITableViewCellAccessoryDetailDisclosureButton] = 68,
            [UITableViewCellAccessoryCheckmark] = 40,
            [UITableViewCellAccessoryDetailButton] = 48
        };
        contentViewWidth -= systemAccessoryWidths[cell.accessoryType];
    }
    
    CGFloat fittingHeight = 0;
    
    if (!cell.fd_enforceFrameLayout && contentViewWidth > 0) {
        //如果是自动布局,则将contentView的宽度约束添加进去。
        //这样做的目的是让UILabel之类的内容可能多行的控件适应这个宽度折行(当然前提是我们已经设置好了这些控件的布局约束)。然后调用systemLayoutSizeFittingSize来计算高度。
        //最后移除刚才临时添加的contentView宽度约束。
        NSLayoutConstraint *widthFenceConstraint = [NSLayoutConstraint constraintWithItem:cell.contentView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:contentViewWidth];
        [cell.contentView addConstraint:widthFenceConstraint];
        
        fittingHeight = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
        [cell.contentView removeConstraint:widthFenceConstraint];
    }
    
    if (fittingHeight == 0) {
        // 尝试调用 '- sizeThatFits:'进行高度计算.
        // 注意:配件高度不应包括分隔线视图高度。
        fittingHeight = [cell sizeThatFits:CGSizeMake(contentViewWidth, 0)].height;
    }
    
    // 进行完前面的逻辑后高度如果仍然为0,则使用默认行高44
    if (fittingHeight == 0) {
        fittingHeight = 44;        
    }
    
    // 添加一像素作为tableView分割线高度。
    if (self.separatorStyle != UITableViewCellSeparatorStyleNone) {
        fittingHeight += 1.0 / [UIScreen mainScreen].scale;
    }
    
    return fittingHeight;
}

至此,就大致将这个框架分析的差不多了,源码中,对类的实例化均为采用runtime添加AssociatedObject的方式。就不做解释了。


最后

上一篇 下一篇

猜你喜欢

热点阅读