Skip to content

Commit 482f940

Browse files
committed
1.1.7.8 兼容
1 parent 56fd3f2 commit 482f940

File tree

2 files changed

+36
-10
lines changed

2 files changed

+36
-10
lines changed

DWTableViewHelper/DWTableViewHelper.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@
123123
124124
version 1.1.7.7.1
125125
修改helper行高返回合法值及优先级
126+
127+
version 1.1.7.8
128+
增加兼容模式,modelFromIndexPath兼容更多异常情况
129+
行高计算修改部分问题
130+
createCell增加兼容模式
126131
*/
127132

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

494+
///仅为了避免异常崩溃而生成的占位model
495+
@property (nonatomic ,assign ,readonly) BOOL placeHolderAvoidCrashing;
496+
489497
/**
490498
当前正在展示的cell
491499

DWTableViewHelper/DWTableViewHelper.m

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@
1616

1717
static UIImage * ImageNull = nil;
1818
const CGFloat DWTableViewHelperAutomaticDimensionAndCache = -91.0702;
19+
20+
@interface DWTableviewHelperPlaceHolderCell : DWTableViewHelperCell
21+
22+
@end
23+
24+
@implementation DWTableviewHelperPlaceHolderCell
25+
26+
@end
27+
1928
@interface DWTableViewHelper ()<UITableViewDelegate,UITableViewDataSource,UITableViewDataSourcePrefetching>
2029
{
2130
BOOL hasPlaceHolderView;
@@ -59,6 +68,8 @@ @interface DWTableViewHelperModel ()
5968

6069
@property (nonatomic ,strong) NSIndexPath * currentDisplayIndexPath;
6170

71+
@property (nonatomic ,assign) BOOL placeHolderAvoidCrashing;
72+
6273
@end
6374

6475
@implementation DWTableViewHelper
@@ -90,19 +101,19 @@ -(__kindof DWTableViewHelperModel *)modelFromIndexPath:(NSIndexPath *)indexPath
90101
if (self.multiSection) {
91102
if (indexPath.section >= self.dataSource.count) {
92103
NSAssert(NO, @"can't fetch model at indexPath(%ld-%ld) for currentDataSource count is %ld",indexPath.section,indexPath.row,self.dataSource.count);
93-
return nil;
104+
return PlaceHolderCellModelAvoidCrashingGetter();
94105
}
95106
obj = self.dataSource[indexPath.section];
96107
if (![obj isKindOfClass:[NSArray class]]) {
97108
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]));
98109
if ([obj isKindOfClass:[DWTableViewHelperModel class]]) {
99110
return obj;
100111
}
101-
return nil;
112+
return PlaceHolderCellModelAvoidCrashingGetter();
102113
}
103114
if (indexPath.row >= [obj count]) {
104115
NSAssert(NO, @"can't fetch model at indexPath(%ld-%ld) for currentDataSource subArr count is %ld",indexPath.section,indexPath.row,[obj count]);
105-
return nil;
116+
return PlaceHolderCellModelAvoidCrashingGetter();
106117
}
107118
obj = self.dataSource[indexPath.section][indexPath.row];
108119
if (![obj isKindOfClass:[DWTableViewHelperModel class]]) {
@@ -112,7 +123,7 @@ -(__kindof DWTableViewHelperModel *)modelFromIndexPath:(NSIndexPath *)indexPath
112123
} else {
113124
if (indexPath.row >= self.dataSource.count) {
114125
NSAssert(NO, @"can't fetch model at indexPath(%ld-%ld) for currentDataSource count is %ld",indexPath.section,indexPath.row,self.dataSource.count);
115-
return nil;
126+
return PlaceHolderCellModelAvoidCrashingGetter();
116127
}
117128
obj = self.dataSource[indexPath.row];
118129
if (![obj isKindOfClass:[DWTableViewHelperModel class]]) {
@@ -1120,6 +1131,11 @@ -(CGFloat)autoCalculateRowHeightWithModel:(__kindof DWTableViewHelperModel *)mod
11201131

11211132
///根据cell计算cell的高度(代码源自FDTemplateLayoutCell)
11221133
-(CGFloat)calculateCellHeightWithCell:(UITableViewCell *)cell {
1134+
1135+
if (!cell || [cell isKindOfClass:[DWTableviewHelperPlaceHolderCell class]]) {
1136+
return 0.01;
1137+
}
1138+
11231139
CGFloat width = self.tabV.bounds.size.width;
11241140

11251141
if (width <= 0) {
@@ -1129,7 +1145,7 @@ -(CGFloat)calculateCellHeightWithCell:(UITableViewCell *)cell {
11291145
CGRect cellBounds = cell.bounds;
11301146
cellBounds.size.width = width;
11311147
cell.bounds = cellBounds;
1132-
CGFloat accessoryViewWidth;
1148+
CGFloat accessoryViewWidth = 0;
11331149

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

11411157
//根据辅助视图校正width
11421158
if (cell.accessoryView) {
1143-
accessoryViewWidth = (cell.accessoryView.bounds.size.width + 16);
1159+
accessoryViewWidth += (cell.accessoryView.bounds.size.width + 16);
11441160
} else {
11451161
static const CGFloat accessoryWidth[] = {
11461162
[UITableViewCellAccessoryNone] = 0,
@@ -1149,7 +1165,7 @@ -(CGFloat)calculateCellHeightWithCell:(UITableViewCell *)cell {
11491165
[UITableViewCellAccessoryCheckmark] = 40,
11501166
[UITableViewCellAccessoryDetailButton] = 48
11511167
};
1152-
accessoryViewWidth = accessoryWidth[cell.accessoryType];
1168+
accessoryViewWidth += accessoryWidth[cell.accessoryType];
11531169
}
11541170

11551171
if ([UIScreen mainScreen].scale >= 3 && [UIScreen mainScreen].bounds.size.width >= 414) {
@@ -1216,7 +1232,8 @@ -(__kindof DWTableViewHelperCell *)createCellFromModel:(DWTableViewHelperModel *
12161232
aCellClassStr = self.cellClassStr;
12171233
} else {
12181234
NSAssert(NO, @"cellClassStr and cellID must be set together at least one time in DWTableViewHelperModel or DWTableViewHelper");
1219-
return nil;
1235+
cellIDTemp = PlaceHolderCellModelAvoidCrashingGetter().cellID;
1236+
aCellClassStr = PlaceHolderCellModelAvoidCrashingGetter().cellClassStr;
12201237
}
12211238
__kindof DWTableViewHelperCell * cell = nil;
12221239
if (useReuse) {
@@ -1234,7 +1251,7 @@ -(__kindof DWTableViewHelperCell *)createCellFromModel:(DWTableViewHelperModel *
12341251
Class cellClass = NSClassFromString(aCellClassStr);
12351252
if (!cellClass) {
12361253
NSAssert(NO, @"cannot load a cellClass from %@,check the cellClassStr you have set",aCellClassStr);
1237-
return nil;
1254+
cellClass = NSClassFromString(PlaceHolderCellModelAvoidCrashingGetter().cellClassStr);
12381255
}
12391256

12401257
if (model.loadCellFromNib) {
@@ -1616,8 +1633,9 @@ static inline void handlePlaceHolderView(UIView * placeHolderView,UITableView *
16161633
if (PlaceHolderCellModelAvoidCrashing == nil) {
16171634
PlaceHolderCellModelAvoidCrashing = [DWTableViewHelperModel new];
16181635
PlaceHolderCellModelAvoidCrashing.cellRowHeight = 0;
1619-
PlaceHolderCellModelAvoidCrashing.cellClassStr = NSStringFromClass([DWTableViewHelperCell class]);
1636+
PlaceHolderCellModelAvoidCrashing.cellClassStr = NSStringFromClass([DWTableviewHelperPlaceHolderCell class]);
16201637
PlaceHolderCellModelAvoidCrashing.cellID = @"PlaceHolderCellAvoidCrashing";
1638+
PlaceHolderCellModelAvoidCrashing.placeHolderAvoidCrashing = YES;
16211639
}
16221640
return PlaceHolderCellModelAvoidCrashing;
16231641
}

0 commit comments

Comments
 (0)