Skip to content

Commit 5f7b4bb

Browse files
committed
修复了当不需要更改隐藏状态依然刷新问题
1 parent 0648eb8 commit 5f7b4bb

6 files changed

Lines changed: 52 additions & 5 deletions

File tree

.DS_Store

0 Bytes
Binary file not shown.

ZHTableViewGroupExample/ZHTableViewGroupObjc/ZHTableViewCell.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,19 @@
2929
@property (nonatomic, copy) CGFloat(^customHeightBlock)(CellType cell, NSIndexPath *indexPath);
3030
@property (nonatomic, copy) BOOL(^hiddenBlock)(NSIndexPath *indexPath);
3131

32+
/// 为指定的索引设置是否隐藏
33+
/// @param hidden 是否隐藏
34+
/// @param indexPath 指定索引
35+
- (void)setHidden:(BOOL)hidden
36+
indexPath:(NSIndexPath *)indexPath;
37+
38+
/// 获取指定的索引已经显示内容是否隐藏 如果不存在默认位显示
39+
/// @param indexPath 指定的索引
40+
- (BOOL)isHiddenDisplayWithIndexPath:(NSIndexPath *)indexPath;
41+
42+
/// 获取最新当前索引是否隐藏
43+
/// @param indexPath 指定索引
44+
- (BOOL)isHiddenWithIndexPath:(NSIndexPath *)indexPath;
3245
/**
3346
* 为了是支持泛型
3447

ZHTableViewGroupExample/ZHTableViewGroupObjc/ZHTableViewCell.m

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99
#import "ZHTableViewCell.h"
1010

1111
@implementation ZHTableViewCell {
12+
NSMutableDictionary<NSString *, NSNumber *> *_hiddenMap;
1213
}
1314

1415
- (instancetype)init {
1516
if (self = [super init]) {
1617
_cellNumber = 1;
18+
_hiddenMap = [NSMutableDictionary dictionary];
1719
}
1820
return self;
1921
}
@@ -51,4 +53,29 @@ - (void)configurationCellWithCellNumber:(NSUInteger)cellNumber identifier:(NSStr
5153
self.didSelectRowCompletionHandle = didSelectRowCompletionHandle;
5254
}
5355

56+
- (void)setHidden:(BOOL)hidden
57+
indexPath:(NSIndexPath *)indexPath {
58+
NSString *key = [self hiddenKeyWithIndexPath:indexPath];
59+
_hiddenMap[key] = @(hidden);
60+
}
61+
62+
- (BOOL)isHiddenDisplayWithIndexPath:(NSIndexPath *)indexPath {
63+
NSString *key = [self hiddenKeyWithIndexPath:indexPath];
64+
NSNumber *hiddenNumber = _hiddenMap[key];
65+
if (!hiddenNumber) {
66+
return NO;
67+
}
68+
return [hiddenNumber boolValue];
69+
}
70+
71+
- (BOOL)isHiddenWithIndexPath:(NSIndexPath *)indexPath {
72+
BOOL isHidden = self.hiddenBlock && self.hiddenBlock(indexPath);
73+
[self setHidden:isHidden indexPath:indexPath];
74+
return isHidden;
75+
}
76+
77+
- (NSString *)hiddenKeyWithIndexPath:(NSIndexPath *)indexPath {
78+
return [NSString stringWithFormat:@"hidden-%@-%@",@(indexPath.section),@(indexPath.row)];
79+
}
80+
5481
@end

ZHTableViewGroupExample/ZHTableViewGroupObjc/ZHTableViewDataSource.m

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ + (CGFloat)heightForRowAtDataSource:(ZHTableViewDataSource *)dataSource
9595
}
9696
NSIndexPath *realyIndexPath = [self indexPathWithDataSource:dataSource
9797
indexPath:indexPath];
98-
if (cell.hiddenBlock && cell.hiddenBlock(realyIndexPath)) {
98+
if ([cell isHiddenWithIndexPath:realyIndexPath]) {
9999
return 0;
100100
}
101101
if (cell.customHeightBlock) {
@@ -602,7 +602,13 @@ - (void)reloadAllHiddenCell {
602602
if (!obj.hiddenBlock) {
603603
return;
604604
}
605-
[needReloadIndexPath addObjectsFromArray:[self indexPathsWithTableViewCell:obj]];
605+
[[self indexPathsWithTableViewCell:obj] enumerateObjectsUsingBlock:^(NSIndexPath * _Nonnull indexPath, NSUInteger idx, BOOL * _Nonnull stop) {
606+
NSIndexPath *realIndexPath = [NSIndexPath indexPathForRow:idx inSection:0];
607+
if ([obj isHiddenDisplayWithIndexPath:realIndexPath] == [obj isHiddenWithIndexPath:realIndexPath]) {
608+
return;
609+
}
610+
[needReloadIndexPath addObject:indexPath];
611+
}];
606612
}] ;
607613
[self updatesTableView:^{
608614
[self.tableView reloadRowsAtIndexPaths:needReloadIndexPath

ZHTableViewGroupExample/ZHTableViewGroupObjc/ZHTableViewGroup.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ typedef void(^ZHTableViewGroupAddHeaderFooterCompletionHandle)(ZHTableViewHeader
2626

2727
- (UITableViewCell *)cellForTableViewWithTableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath config:(BOOL)config;
2828

29-
- (void)addCellWithCompletionHandle:(ZHTableViewGroupAddCellCompletionHandle)completionHandle;
29+
- (ZHTableViewCell *)addCellWithCompletionHandle:(ZHTableViewGroupAddCellCompletionHandle)completionHandle;
3030

3131
- (void)addHeaderWithCompletionHandle:(ZHTableViewGroupAddHeaderFooterCompletionHandle)completionHandle;
3232

ZHTableViewGroupExample/ZHTableViewGroupObjc/ZHTableViewGroup.m

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,13 @@ - (void)registerHeaderFooterCellWithTableView:(UITableView *)tableView {
3030
}
3131
}
3232

33-
- (void)addCellWithCompletionHandle:(ZHTableViewGroupAddCellCompletionHandle)completionHandle {
33+
- (ZHTableViewCell *)addCellWithCompletionHandle:(ZHTableViewGroupAddCellCompletionHandle)completionHandle {
3434
ZHTableViewCell *cell = [[ZHTableViewCell alloc] init];
3535
if (completionHandle) {
3636
completionHandle(cell);
3737
}
3838
[self.cells addObject:cell];
39+
return cell;
3940
}
4041

4142
- (void)addHeaderWithCompletionHandle:(ZHTableViewGroupAddHeaderFooterCompletionHandle)completionHandle {
@@ -67,7 +68,7 @@ - (UITableViewCell *)cellForTableViewWithTableView:(UITableView *)tableView inde
6768
}
6869
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableViewCell.identifier];
6970
NSIndexPath *realIndexPath = [self indexPathWithCell:tableViewCell indexPath:indexPath];
70-
BOOL isHidden = tableViewCell.hiddenBlock && tableViewCell.hiddenBlock(realIndexPath);
71+
BOOL isHidden = [tableViewCell isHiddenWithIndexPath:realIndexPath];
7172
cell.hidden = isHidden;
7273
if (config) {
7374
[self tableViewCell:tableViewCell

0 commit comments

Comments
 (0)