16
16
17
17
static UIImage * ImageNull = nil ;
18
18
const CGFloat DWTableViewHelperAutomaticDimensionAndCache = -91.0702 ;
19
+
20
+ @interface DWTableviewHelperPlaceHolderCell : DWTableViewHelperCell
21
+
22
+ @end
23
+
24
+ @implementation DWTableviewHelperPlaceHolderCell
25
+
26
+ @end
27
+
19
28
@interface DWTableViewHelper ()<UITableViewDelegate,UITableViewDataSource,UITableViewDataSourcePrefetching>
20
29
{
21
30
BOOL hasPlaceHolderView;
@@ -59,6 +68,8 @@ @interface DWTableViewHelperModel ()
59
68
60
69
@property (nonatomic ,strong ) NSIndexPath * currentDisplayIndexPath;
61
70
71
+ @property (nonatomic ,assign ) BOOL placeHolderAvoidCrashing;
72
+
62
73
@end
63
74
64
75
@implementation DWTableViewHelper
@@ -90,19 +101,19 @@ -(__kindof DWTableViewHelperModel *)modelFromIndexPath:(NSIndexPath *)indexPath
90
101
if (self.multiSection ) {
91
102
if (indexPath.section >= self.dataSource .count ) {
92
103
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 () ;
94
105
}
95
106
obj = self.dataSource [indexPath.section];
96
107
if (![obj isKindOfClass: [NSArray class ]]) {
97
108
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 ]));
98
109
if ([obj isKindOfClass: [DWTableViewHelperModel class ]]) {
99
110
return obj;
100
111
}
101
- return nil ;
112
+ return PlaceHolderCellModelAvoidCrashingGetter () ;
102
113
}
103
114
if (indexPath.row >= [obj count ]) {
104
115
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 () ;
106
117
}
107
118
obj = self.dataSource [indexPath.section][indexPath.row];
108
119
if (![obj isKindOfClass: [DWTableViewHelperModel class ]]) {
@@ -112,7 +123,7 @@ -(__kindof DWTableViewHelperModel *)modelFromIndexPath:(NSIndexPath *)indexPath
112
123
} else {
113
124
if (indexPath.row >= self.dataSource .count ) {
114
125
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 () ;
116
127
}
117
128
obj = self.dataSource [indexPath.row];
118
129
if (![obj isKindOfClass: [DWTableViewHelperModel class ]]) {
@@ -1120,6 +1131,11 @@ -(CGFloat)autoCalculateRowHeightWithModel:(__kindof DWTableViewHelperModel *)mod
1120
1131
1121
1132
// /根据cell计算cell的高度(代码源自FDTemplateLayoutCell)
1122
1133
-(CGFloat)calculateCellHeightWithCell : (UITableViewCell *)cell {
1134
+
1135
+ if (!cell || [cell isKindOfClass: [DWTableviewHelperPlaceHolderCell class ]]) {
1136
+ return 0.01 ;
1137
+ }
1138
+
1123
1139
CGFloat width = self.tabV .bounds .size .width ;
1124
1140
1125
1141
if (width <= 0 ) {
@@ -1129,7 +1145,7 @@ -(CGFloat)calculateCellHeightWithCell:(UITableViewCell *)cell {
1129
1145
CGRect cellBounds = cell.bounds ;
1130
1146
cellBounds.size .width = width;
1131
1147
cell.bounds = cellBounds;
1132
- CGFloat accessoryViewWidth;
1148
+ CGFloat accessoryViewWidth = 0 ;
1133
1149
1134
1150
for (UIView *view in self.tabV .subviews ) {
1135
1151
if ([view isKindOfClass: NSClassFromString (@" UITableViewIndex" )]) {
@@ -1140,7 +1156,7 @@ -(CGFloat)calculateCellHeightWithCell:(UITableViewCell *)cell {
1140
1156
1141
1157
// 根据辅助视图校正width
1142
1158
if (cell.accessoryView ) {
1143
- accessoryViewWidth = (cell.accessoryView .bounds .size .width + 16 );
1159
+ accessoryViewWidth + = (cell.accessoryView .bounds .size .width + 16 );
1144
1160
} else {
1145
1161
static const CGFloat accessoryWidth[] = {
1146
1162
[UITableViewCellAccessoryNone] = 0 ,
@@ -1149,7 +1165,7 @@ -(CGFloat)calculateCellHeightWithCell:(UITableViewCell *)cell {
1149
1165
[UITableViewCellAccessoryCheckmark] = 40 ,
1150
1166
[UITableViewCellAccessoryDetailButton] = 48
1151
1167
};
1152
- accessoryViewWidth = accessoryWidth[cell.accessoryType];
1168
+ accessoryViewWidth + = accessoryWidth[cell.accessoryType];
1153
1169
}
1154
1170
1155
1171
if ([UIScreen mainScreen ].scale >= 3 && [UIScreen mainScreen ].bounds .size .width >= 414 ) {
@@ -1216,7 +1232,8 @@ -(__kindof DWTableViewHelperCell *)createCellFromModel:(DWTableViewHelperModel *
1216
1232
aCellClassStr = self.cellClassStr ;
1217
1233
} else {
1218
1234
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 ;
1220
1237
}
1221
1238
__kindof DWTableViewHelperCell * cell = nil ;
1222
1239
if (useReuse) {
@@ -1234,7 +1251,7 @@ -(__kindof DWTableViewHelperCell *)createCellFromModel:(DWTableViewHelperModel *
1234
1251
Class cellClass = NSClassFromString (aCellClassStr);
1235
1252
if (!cellClass) {
1236
1253
NSAssert (NO , @" cannot load a cellClass from %@ ,check the cellClassStr you have set" ,aCellClassStr);
1237
- return nil ;
1254
+ cellClass = NSClassFromString ( PlaceHolderCellModelAvoidCrashingGetter (). cellClassStr ) ;
1238
1255
}
1239
1256
1240
1257
if (model.loadCellFromNib ) {
@@ -1616,8 +1633,9 @@ static inline void handlePlaceHolderView(UIView * placeHolderView,UITableView *
1616
1633
if (PlaceHolderCellModelAvoidCrashing == nil ) {
1617
1634
PlaceHolderCellModelAvoidCrashing = [DWTableViewHelperModel new ];
1618
1635
PlaceHolderCellModelAvoidCrashing.cellRowHeight = 0 ;
1619
- PlaceHolderCellModelAvoidCrashing.cellClassStr = NSStringFromClass ([DWTableViewHelperCell class ]);
1636
+ PlaceHolderCellModelAvoidCrashing.cellClassStr = NSStringFromClass ([DWTableviewHelperPlaceHolderCell class ]);
1620
1637
PlaceHolderCellModelAvoidCrashing.cellID = @" PlaceHolderCellAvoidCrashing" ;
1638
+ PlaceHolderCellModelAvoidCrashing.placeHolderAvoidCrashing = YES ;
1621
1639
}
1622
1640
return PlaceHolderCellModelAvoidCrashing;
1623
1641
}
0 commit comments