Skip to content

Commit 982c323

Browse files
committed
修复 #6
1 parent f6b5602 commit 982c323

4 files changed

Lines changed: 78 additions & 60 deletions

File tree

Example/UITableViewDynamicLayoutCacheHeight/Classes/Controller/BMHomeVC.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
6565
}
6666

6767
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
68+
// return 100;
6869
if (kTEST_KEY_CACHE) {
6970
return [tableView bm_heightWithCellClass:BMCell.class cacheByKey:indexPath.description configuration:^(__kindof BMCell *cell) {
7071
// 配置 Cell

UITableViewDynamicLayoutCacheHeight.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = 'UITableViewDynamicLayoutCacheHeight'
3-
s.version = '5.1.0'
3+
s.version = '5.1.1'
44
s.summary = '🖖 Template auto layout cell for automatically UITableViewCell UITableViewHeaderFooterView calculating and cache height framework.'
55
s.homepage = 'https://github.com/liangdahong/UITableViewDynamicLayoutCacheHeight'
66
s.license = 'MIT'

UITableViewDynamicLayoutCacheHeight/Classes/Private/UITableView+BMPrivate.m

Lines changed: 75 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ + (void)load {
221221
@selector(reloadRowsAtIndexPaths:withRowAnimation:),
222222
@selector(moveRowAtIndexPath:toIndexPath:)
223223
};
224-
224+
225225
for (NSUInteger index = 0; index < sizeof(selectors) / sizeof(SEL); ++index) {
226226
SEL originalSelector = selectors[index];
227227
SEL swizzledSelector = NSSelectorFromString([@"tableView_dynamicLayout_" stringByAppendingString:NSStringFromSelector(originalSelector)]);
@@ -241,91 +241,107 @@ - (void)tableView_dynamicLayout_reloadData {
241241
}
242242

243243
- (void)tableView_dynamicLayout_insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation {
244-
// 清空缓存数据,这里可以优化,由于需要考虑太多的情况,暂时没有提供全面的测试方法,暂时直接全部刷新。
245-
[self _initCacheArrayWithDataSource:self.dataSource];
246-
kChangedCacheLog
244+
if (self.isDynamicLayoutInitializationed) {
245+
// 清空缓存数据,这里可以优化,由于需要考虑太多的情况,暂时没有提供全面的测试方法,暂时直接全部刷新。
246+
[self _initCacheArrayWithDataSource:self.dataSource];
247+
kChangedCacheLog
248+
}
247249
[self tableView_dynamicLayout_insertSections:sections withRowAnimation:animation];
248250
}
249251

250252
- (void)tableView_dynamicLayout_deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation {
251-
[sections enumerateIndexesWithOptions:(NSEnumerationReverse) usingBlock:^(NSUInteger section, BOOL * _Nonnull stop) {
252-
// cell
253-
[self.verticalArray removeObjectAtIndex:section];
254-
[self.horizontalArray removeObjectAtIndex:section];
255-
// header footer
256-
[self.headerVerticalArray removeObjectAtIndex:section];
257-
[self.headerHorizontalArray removeObjectAtIndex:section];
258-
[self.footerVerticalArray removeObjectAtIndex:section];
259-
[self.footerHorizontalArray removeObjectAtIndex:section];
260-
}];
261-
kChangedCacheLog
253+
if (self.isDynamicLayoutInitializationed) {
254+
[sections enumerateIndexesWithOptions:(NSEnumerationReverse) usingBlock:^(NSUInteger section, BOOL * _Nonnull stop) {
255+
// cell
256+
[self.verticalArray removeObjectAtIndex:section];
257+
[self.horizontalArray removeObjectAtIndex:section];
258+
// header footer
259+
[self.headerVerticalArray removeObjectAtIndex:section];
260+
[self.headerHorizontalArray removeObjectAtIndex:section];
261+
[self.footerVerticalArray removeObjectAtIndex:section];
262+
[self.footerHorizontalArray removeObjectAtIndex:section];
263+
}];
264+
kChangedCacheLog
265+
}
262266
[self tableView_dynamicLayout_deleteSections:sections withRowAnimation:animation];
263267
}
264268

265269
- (void)tableView_dynamicLayout_reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation {
266-
[sections enumerateIndexesUsingBlock:^(NSUInteger section, BOOL * _Nonnull stop) {
267-
// 组的数据可能改变 需要重新获取组的行数
268-
NSInteger sectionCount = [self.dataSource tableView:self numberOfRowsInSection:section];
269-
NSMutableArray *arr = [NSMutableArray arrayWithCapacity:sectionCount];
270-
while (sectionCount-- > 0) {
271-
[arr addObject:kDefaultHeight];
272-
}
273-
self.verticalArray[section] = arr.mutableCopy;
274-
self.horizontalArray[section] = arr.mutableCopy;
275-
276-
// header footer
277-
self.headerVerticalArray[section] = kDefaultHeight;
278-
self.headerHorizontalArray[section] = kDefaultHeight;
279-
self.footerVerticalArray[section] = kDefaultHeight;
280-
self.footerHorizontalArray[section] = kDefaultHeight;
281-
}];
282-
kChangedCacheLog
270+
if (self.isDynamicLayoutInitializationed) {
271+
[sections enumerateIndexesUsingBlock:^(NSUInteger section, BOOL * _Nonnull stop) {
272+
// 组的数据可能改变 需要重新获取组的行数
273+
NSInteger sectionCount = [self.dataSource tableView:self numberOfRowsInSection:section];
274+
NSMutableArray *arr = [NSMutableArray arrayWithCapacity:sectionCount];
275+
while (sectionCount-- > 0) {
276+
[arr addObject:kDefaultHeight];
277+
}
278+
self.verticalArray[section] = arr.mutableCopy;
279+
self.horizontalArray[section] = arr.mutableCopy;
280+
281+
// header footer
282+
self.headerVerticalArray[section] = kDefaultHeight;
283+
self.headerHorizontalArray[section] = kDefaultHeight;
284+
self.footerVerticalArray[section] = kDefaultHeight;
285+
self.footerHorizontalArray[section] = kDefaultHeight;
286+
}];
287+
kChangedCacheLog
288+
}
283289
[self tableView_dynamicLayout_reloadSections:sections withRowAnimation:animation];
284290
}
285291

286292
- (void)tableView_dynamicLayout_moveSection:(NSInteger)section toSection:(NSInteger)newSection {
287-
// 清空缓存数据,这里可以优化,由于需要考虑太多的情况,暂时没有提供全面的测试方法,暂时直接全部刷新。
288-
[self _initCacheArrayWithDataSource:self.dataSource];
289-
kChangedCacheLog
293+
if (self.isDynamicLayoutInitializationed) {
294+
// 清空缓存数据,这里可以优化,由于需要考虑太多的情况,暂时没有提供全面的测试方法,暂时直接全部刷新。
295+
[self _initCacheArrayWithDataSource:self.dataSource];
296+
kChangedCacheLog
297+
}
290298
[self tableView_dynamicLayout_moveSection:section toSection:newSection];
291299
}
292300

293301
- (void)tableView_dynamicLayout_insertRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation {
294-
// 清空缓存数据,这里可以优化,由于需要考虑太多的情况,暂时没有提供全面的测试方法,暂时直接全部刷新。
295-
[self _initCacheArrayWithDataSource:self.dataSource];
296-
kChangedCacheLog
302+
if (self.isDynamicLayoutInitializationed) {
303+
// 清空缓存数据,这里可以优化,由于需要考虑太多的情况,暂时没有提供全面的测试方法,暂时直接全部刷新。
304+
[self _initCacheArrayWithDataSource:self.dataSource];
305+
kChangedCacheLog
306+
}
297307
[self tableView_dynamicLayout_insertRowsAtIndexPaths:indexPaths withRowAnimation:animation];
298308
}
299309

300310
- (void)tableView_dynamicLayout_deleteRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation {
301-
NSMutableArray *tempIndexPaths = indexPaths.mutableCopy;
302-
[tempIndexPaths sortUsingComparator:^NSComparisonResult(NSIndexPath * _Nonnull obj1, NSIndexPath * _Nonnull obj2) {
303-
if (obj1.section == obj2.section) {
304-
return obj1.row < obj2.row;
305-
}
306-
return obj1.section < obj2.section;
307-
}];
308-
[tempIndexPaths enumerateObjectsUsingBlock:^(NSIndexPath * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
309-
[self.verticalArray[obj.section] removeObjectAtIndex:obj.row];
310-
[self.horizontalArray[obj.section] removeObjectAtIndex:obj.row];
311-
}];
312-
kChangedCacheLog
311+
if (self.isDynamicLayoutInitializationed) {
312+
NSMutableArray *tempIndexPaths = indexPaths.mutableCopy;
313+
[tempIndexPaths sortUsingComparator:^NSComparisonResult(NSIndexPath * _Nonnull obj1, NSIndexPath * _Nonnull obj2) {
314+
if (obj1.section == obj2.section) {
315+
return obj1.row < obj2.row;
316+
}
317+
return obj1.section < obj2.section;
318+
}];
319+
[tempIndexPaths enumerateObjectsUsingBlock:^(NSIndexPath * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
320+
[self.verticalArray[obj.section] removeObjectAtIndex:obj.row];
321+
[self.horizontalArray[obj.section] removeObjectAtIndex:obj.row];
322+
}];
323+
kChangedCacheLog
324+
}
313325
[self tableView_dynamicLayout_deleteRowsAtIndexPaths:indexPaths withRowAnimation:animation];
314326
}
315327

316328
- (void)tableView_dynamicLayout_reloadRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation {
317-
[indexPaths enumerateObjectsUsingBlock:^(NSIndexPath * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
318-
self.verticalArray[obj.section][obj.row] = kDefaultHeight;
319-
self.horizontalArray[obj.section][obj.row] = kDefaultHeight;
320-
}];
321-
kChangedCacheLog
329+
if (self.isDynamicLayoutInitializationed) {
330+
[indexPaths enumerateObjectsUsingBlock:^(NSIndexPath * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
331+
self.verticalArray[obj.section][obj.row] = kDefaultHeight;
332+
self.horizontalArray[obj.section][obj.row] = kDefaultHeight;
333+
}];
334+
kChangedCacheLog
335+
}
322336
[self tableView_dynamicLayout_reloadRowsAtIndexPaths:indexPaths withRowAnimation:animation];
323337
}
324338

325339
- (void)tableView_dynamicLayout_moveRowAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath {
326-
// 清空缓存数据,这里可以优化,由于需要考虑太多的情况,暂时没有提供全面的测试方法,暂时直接全部刷新。
327-
[self _initCacheArrayWithDataSource:self.dataSource];
328-
kChangedCacheLog
340+
if (self.isDynamicLayoutInitializationed) {
341+
// 清空缓存数据,这里可以优化,由于需要考虑太多的情况,暂时没有提供全面的测试方法,暂时直接全部刷新。
342+
[self _initCacheArrayWithDataSource:self.dataSource];
343+
kChangedCacheLog
344+
}
329345
[self tableView_dynamicLayout_moveRowAtIndexPath:indexPath toIndexPath:newIndexPath];
330346
}
331347

@@ -402,3 +418,4 @@ - (void)_changedCacheLog {
402418
}
403419

404420
@end
421+

UITableViewDynamicLayoutCacheHeight/Classes/UITableViewDynamicLayoutCacheHeight.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
// SOFTWARE.
2222

2323
////////////////
24-
/// v5.1.0
24+
/// v5.1.1
2525
////////////////
2626

2727
#ifndef UITableViewDynamicLayoutCacheHeight_h

0 commit comments

Comments
 (0)