Skip to content

Commit

Permalink
1.1.7.8 兼容
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeWicky committed Jun 17, 2020
1 parent 56fd3f2 commit 482f940
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
8 changes: 8 additions & 0 deletions DWTableViewHelper/DWTableViewHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@
version 1.1.7.7.1
修改helper行高返回合法值及优先级
version 1.1.7.8
增加兼容模式,modelFromIndexPath兼容更多异常情况
行高计算修改部分问题
createCell增加兼容模式
*/

#import <UIKit/UIKit.h>
Expand Down Expand Up @@ -486,6 +491,9 @@ typedef NS_ENUM(NSUInteger, DWTableViewHelperLoadDataMode) {///数据加载优
///配合DWTableViewHelperLoadDataIgnoreHighSpeedMode使用,标志cell是否被绘制过
@property (nonatomic ,assign ,readonly) BOOL cellHasBeenDrawn;

///仅为了避免异常崩溃而生成的占位model
@property (nonatomic ,assign ,readonly) BOOL placeHolderAvoidCrashing;

/**
当前正在展示的cell
Expand Down
38 changes: 28 additions & 10 deletions DWTableViewHelper/DWTableViewHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@

static UIImage * ImageNull = nil;
const CGFloat DWTableViewHelperAutomaticDimensionAndCache = -91.0702;

@interface DWTableviewHelperPlaceHolderCell : DWTableViewHelperCell

@end

@implementation DWTableviewHelperPlaceHolderCell

@end

@interface DWTableViewHelper ()<UITableViewDelegate,UITableViewDataSource,UITableViewDataSourcePrefetching>
{
BOOL hasPlaceHolderView;
Expand Down Expand Up @@ -59,6 +68,8 @@ @interface DWTableViewHelperModel ()

@property (nonatomic ,strong) NSIndexPath * currentDisplayIndexPath;

@property (nonatomic ,assign) BOOL placeHolderAvoidCrashing;

@end

@implementation DWTableViewHelper
Expand Down Expand Up @@ -90,19 +101,19 @@ -(__kindof DWTableViewHelperModel *)modelFromIndexPath:(NSIndexPath *)indexPath
if (self.multiSection) {
if (indexPath.section >= self.dataSource.count) {
NSAssert(NO, @"can't fetch model at indexPath(%ld-%ld) for currentDataSource count is %ld",indexPath.section,indexPath.row,self.dataSource.count);
return nil;
return PlaceHolderCellModelAvoidCrashingGetter();
}
obj = self.dataSource[indexPath.section];
if (![obj isKindOfClass:[NSArray class]]) {
NSAssert(NO, @"you set to use multiSection but the obj in section %ld of dataSource is not kind of NSArray but %@",indexPath.section,NSStringFromClass([obj class]));
if ([obj isKindOfClass:[DWTableViewHelperModel class]]) {
return obj;
}
return nil;
return PlaceHolderCellModelAvoidCrashingGetter();
}
if (indexPath.row >= [obj count]) {
NSAssert(NO, @"can't fetch model at indexPath(%ld-%ld) for currentDataSource subArr count is %ld",indexPath.section,indexPath.row,[obj count]);
return nil;
return PlaceHolderCellModelAvoidCrashingGetter();
}
obj = self.dataSource[indexPath.section][indexPath.row];
if (![obj isKindOfClass:[DWTableViewHelperModel class]]) {
Expand All @@ -112,7 +123,7 @@ -(__kindof DWTableViewHelperModel *)modelFromIndexPath:(NSIndexPath *)indexPath
} else {
if (indexPath.row >= self.dataSource.count) {
NSAssert(NO, @"can't fetch model at indexPath(%ld-%ld) for currentDataSource count is %ld",indexPath.section,indexPath.row,self.dataSource.count);
return nil;
return PlaceHolderCellModelAvoidCrashingGetter();
}
obj = self.dataSource[indexPath.row];
if (![obj isKindOfClass:[DWTableViewHelperModel class]]) {
Expand Down Expand Up @@ -1120,6 +1131,11 @@ -(CGFloat)autoCalculateRowHeightWithModel:(__kindof DWTableViewHelperModel *)mod

///根据cell计算cell的高度(代码源自FDTemplateLayoutCell)
-(CGFloat)calculateCellHeightWithCell:(UITableViewCell *)cell {

if (!cell || [cell isKindOfClass:[DWTableviewHelperPlaceHolderCell class]]) {
return 0.01;
}

CGFloat width = self.tabV.bounds.size.width;

if (width <= 0) {
Expand All @@ -1129,7 +1145,7 @@ -(CGFloat)calculateCellHeightWithCell:(UITableViewCell *)cell {
CGRect cellBounds = cell.bounds;
cellBounds.size.width = width;
cell.bounds = cellBounds;
CGFloat accessoryViewWidth;
CGFloat accessoryViewWidth = 0;

for (UIView *view in self.tabV.subviews) {
if ([view isKindOfClass:NSClassFromString(@"UITableViewIndex")]) {
Expand All @@ -1140,7 +1156,7 @@ -(CGFloat)calculateCellHeightWithCell:(UITableViewCell *)cell {

//根据辅助视图校正width
if (cell.accessoryView) {
accessoryViewWidth = (cell.accessoryView.bounds.size.width + 16);
accessoryViewWidth += (cell.accessoryView.bounds.size.width + 16);
} else {
static const CGFloat accessoryWidth[] = {
[UITableViewCellAccessoryNone] = 0,
Expand All @@ -1149,7 +1165,7 @@ -(CGFloat)calculateCellHeightWithCell:(UITableViewCell *)cell {
[UITableViewCellAccessoryCheckmark] = 40,
[UITableViewCellAccessoryDetailButton] = 48
};
accessoryViewWidth = accessoryWidth[cell.accessoryType];
accessoryViewWidth += accessoryWidth[cell.accessoryType];
}

if ([UIScreen mainScreen].scale >= 3 && [UIScreen mainScreen].bounds.size.width >= 414) {
Expand Down Expand Up @@ -1216,7 +1232,8 @@ -(__kindof DWTableViewHelperCell *)createCellFromModel:(DWTableViewHelperModel *
aCellClassStr = self.cellClassStr;
} else {
NSAssert(NO, @"cellClassStr and cellID must be set together at least one time in DWTableViewHelperModel or DWTableViewHelper");
return nil;
cellIDTemp = PlaceHolderCellModelAvoidCrashingGetter().cellID;
aCellClassStr = PlaceHolderCellModelAvoidCrashingGetter().cellClassStr;
}
__kindof DWTableViewHelperCell * cell = nil;
if (useReuse) {
Expand All @@ -1234,7 +1251,7 @@ -(__kindof DWTableViewHelperCell *)createCellFromModel:(DWTableViewHelperModel *
Class cellClass = NSClassFromString(aCellClassStr);
if (!cellClass) {
NSAssert(NO, @"cannot load a cellClass from %@,check the cellClassStr you have set",aCellClassStr);
return nil;
cellClass = NSClassFromString(PlaceHolderCellModelAvoidCrashingGetter().cellClassStr);
}

if (model.loadCellFromNib) {
Expand Down Expand Up @@ -1616,8 +1633,9 @@ static inline void handlePlaceHolderView(UIView * placeHolderView,UITableView *
if (PlaceHolderCellModelAvoidCrashing == nil) {
PlaceHolderCellModelAvoidCrashing = [DWTableViewHelperModel new];
PlaceHolderCellModelAvoidCrashing.cellRowHeight = 0;
PlaceHolderCellModelAvoidCrashing.cellClassStr = NSStringFromClass([DWTableViewHelperCell class]);
PlaceHolderCellModelAvoidCrashing.cellClassStr = NSStringFromClass([DWTableviewHelperPlaceHolderCell class]);
PlaceHolderCellModelAvoidCrashing.cellID = @"PlaceHolderCellAvoidCrashing";
PlaceHolderCellModelAvoidCrashing.placeHolderAvoidCrashing = YES;
}
return PlaceHolderCellModelAvoidCrashing;
}
Expand Down

0 comments on commit 482f940

Please sign in to comment.