UITableViewCell圖片文本混排
SDAutoLayout學習分析
獲取cell高度的原理
在UITableView+SDAutoTableViewCellHeight分類中,使用SDCellAutoHeightManager來對高度進行管理
- (SDCellAutoHeightManager *)cellAutoHeightManager {
SDCellAutoHeightManager *cellAutoHeightManager = object_getAssociateObject(self, _cmd);
if (!cellAutoHeightManager) {
cellAutoHeightManager = [[SDCellAutoHeightManager alloc] init];
[[self setCellAutoHeightManager: cellAutoHeightManager]];
}
return cellAutoHeightManager;
}
- (void)setCellAutoHeightManager:(SDCellAutoHeightManager *)cellAutoHeightManager{
objc_setAssociateObject(self, @selector(cellAutoHeightManager), cellAutoHeightManager, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
通過使用runtime的objc_setAssociateObject,objc_getAssociateObject來為當前的tableView添加關聯(lián)屬性的高度管理器SDCellAutoHeightManager
在cell中的設置model方法中,例如DemoVC14Cell.m中
//DemoVC14Cell.m
- (void)setModel:(DemoVC7Model *)model {
//...設置UI的相關屬性
[self setupAutoHeightWithBottomView:bottomView bottomMargin:10];
}
//UIView+SDAutoHeightWidth
- (void)setupAutoHeightWithBottomView:(UIView *)bottomView bottomMargin:(CGFloat)bottomMargin {
if(!bottomView) return;
[self setupAutoHeightWithBottomViewsArray:@[bottomView] bottomMargin:bottomMargin];
}
- (void)setupAutoHeightWithBottomViewsArray:(NSArray *)bottomViewsArray bottomMarigin:(CGFloat)bottomMargin {
if(!bottomViewsArray) return;
//清空之前的view
[self.sd_bottomViewsArray removeAllObjects];
[self.sd_bottomViewsArray addObjectsFromArray:bottomViewsArray];
self.sd_bottomViewBottomMargin = bottomMargin;
}
//上面三個方法只有一個目的,那就是確定最底部的view,以及 margin,為后面layoutSubview布局是給autoHeight賦值時做準備。
//DemoVC14.m
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
DemoVC7Model *demoVC14Model = self.modelsArray[indexPath.row];
return [self.tableView cellHeightForIndexPath:indexPath model:demoVC14Model keyPath:@"model" cellClass:[DemoVC14Cell class] contentViewWidth:[self cellContentViewWidth]];
}
//UITableView+SDAutoTableViewCellHeight
- (CGFloat)cellHeightForIndexPath:(NSIndexPath *)indexPath model:(id)model keyPath:(NSString *)keyPath cellClass:(Class)cellClass contentViewWidth:(CGFloat)contentViewWidth{
self.cellAutoHeightManager.modelTableview = self;
self.cellAutoHeightManager.contentViewWidth = contentViewWidth;
return [self.cellAutoHeightManager cellHeightForIndexPath:indexPath model:model keyPath:keyPath cellClass:cellClass];
}
- (CGFloat)cellHeightForIndexPath:(NSIndexPath *)indexPath model:(id)model keyPath:(NSString *)keyPath
{
//取已經(jīng)緩存下的對應的高度,如果有,則直接返回之前計算過的高度
NSNumber *cacheHeight = [self heightCacheForIndexPath:indexPath];
if (cacheHeight) {
return [cacheHeight floatValue];
} else {
if (!self.modelCell) {
return 0;
}
if (self.modelTableview && self.modelTableview != self.modelCell.sd_tableView) {
self.modelCell.sd_tableView = self.modelTableview;
}
self.modelCell.sd_indexPath = indexPath;
if (model && keyPath) {
[self.modelCell setValue:model forKey:keyPath]; //這個地方其實就是調用的setModel方法,在里面會調用上面提到的三個方法
} else if (self.cellDataSetting) {
self.cellDataSetting(self.modelCell);
}
//...
//調用layoutSubviews方法,由于在UIView+SDAutoLayout分類中已經(jīng)使用runtime交換了兩個方法,所以,會在那里面調用自定義的方法,計算出autoHeight
[self.modelCell.contentView layoutSubviews];
//對計算出來的autoHeight進行存儲,方便下次調用
NSString *cacheKey = [self cacheKeyForIndexPath:indexPath];
[_cacheDictionary setObject:@(self.modelCell.autoHeight) forKey:cacheKey];
if (self.modelCell.sd_indexPath && self.modelCell.sd_tableView) {
if (self.modelCell.contentView.shouldReadjustFrameBeforeStoreCache) {
self.modelCell.contentView.height_sd = self.modelCell.autoHeight;
[self.modelCell.contentView layoutSubviews];
}
[self.modelCell.contentView.autoLayoutModelsArray enumerateObjectsUsingBlock:^(SDAutoLayoutModel *model, NSUInteger idx, BOOL *stop) {
//不太明白這個地方,難道是將每個子視圖的frame存儲起來,下次直接調用?
[self.modelTableview.cellAutoHeightManager setSubviewFrameCache:model.needsAutoResizeView.frame WithIndexPath:self.modelCell.sd_indexPath];
}];
}
return self.modelCell.autoHeight;
}
}
使用runtime交換兩個方法
UIView+SDAutoLayout中的load方法
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSArray *selStringsArray = @[@"layoutSubviews"];
[selStringsArray enumerateObjectsUsingBlock:^(NSString *selString, NSUInteger idx, BOOL *stop) {
NSString *mySelString = [@"sd_" stringByAppendingString:selString];
Method originalMethod = class_getInstanceMethod(self, NSSelectorFromString(selString));
Method myMethod = class_getInstanceMethod(self, NSSelectorFromString(mySelString));
method_exchangeImplementations(originalMethod, myMethod); //交換系統(tǒng)的layoutSubviews方法與自己定義的sd_layoutSubviews方法
}];
});
}
- (void)sd_layoutSubviews {
[self sd_layoutSubviews]; //由于已經(jīng)交換了,所以,這行代碼會去調用系統(tǒng)自帶的layoutSubviews方法
[self sd_layoutSubviewsHandle]; //執(zhí)行自己定義的layoutSubviews方法
}
UITableView+SDAutoTableViewCellHeight中的load方法
將tableView中涉及到數(shù)據(jù)源改變的方法進行自定義交換
+(void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSArray *selStringsArray = @[@"reloadData", @"reloadRowsAtIndexPaths:withRowAnimation:",@"deleteRowsAtIndexPaths:withRowAnimation:",@"insertRowsAtIndexPaths:withRowAnimation:"];
[selStringsArray enumerateObjectsUsingBlock:^(NSString *selString, NSUInteger idx, BOOL *stop) {
NSString *mySelString = [@"sd_" stringByAppendingString:selString];
Method originalMethod = class_getInstanceMethod(self, NSSelectorFromString(selString));
Method myMethod = class_getInstanceMethod(self, NSSelectorFromString(mySelString));
method_exchangeImplementtations(originalMethod, myMethod); //交換需要交換的方法
}];
});
}
- (void)sd_reloadData {
if(!self.cellAutoHeightManager.shouldKeepHeightCacheWhenReloadingData) {
[self.cellAutoHeightManager clearHeightCache]; //清除緩存的高度
}
[self sd_reloadData]; //調用系統(tǒng)方法reloadData
self.cellAutoHeightManager.shouldKeepHeightCacheWhenReloadingData = NO;
}
//...自定義其他需要交換的方法
難道是在返回高度方法調用的時候,將對應的約束條件都存儲起來,然后在cellForRow調用的時候再直接使用?