Skip to content

Commit

Permalink
实现删除某行cell时自动调整height缓存
Browse files Browse the repository at this point in the history
  • Loading branch information
gsdios committed Jun 23, 2016
1 parent 0731a55 commit 39aa83c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 6 deletions.
Binary file not shown.
6 changes: 6 additions & 0 deletions SDAutoLayoutDemo/DemoVC/DemoVC5/DemoVC5.m
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,12 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.modelsArray removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// >>>>>>>>>>>>>>>>>>>>> * cell自适应步骤2 * >>>>>>>>>>>>>>>>>>>>>>>>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ typedef void (^AutoCellHeightDataSettingBlock)(UITableViewCell *cell);

- (void)clearHeightCacheOfIndexPaths:(NSArray *)indexPaths;

- (void)deleteThenResetHeightCache:(NSIndexPath *)indexPathToDelete;

- (NSNumber *)heightCacheForIndexPath:(NSIndexPath *)indexPath;

- (CGFloat)cellHeightForIndexPath:(NSIndexPath *)indexPath model:(id)model keyPath:(NSString *)keyPath;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,36 @@ - (void)clearHeightCacheOfIndexPaths:(NSArray *)indexPaths
}];
}

- (void)deleteThenResetHeightCache:(NSIndexPath *)indexPathToDelete
{

NSString *cacheKey = [self cacheKeyForIndexPath:indexPathToDelete];
[_cacheDictionary removeObjectForKey:cacheKey];
[_subviewFrameCacheDict removeObjectForKey:cacheKey];

long sectionOfToDeleteItem = indexPathToDelete.section;
long rowOfToDeleteItem = indexPathToDelete.row;
NSMutableDictionary *tempHeightCacheDict = [NSMutableDictionary new];
NSMutableDictionary *tempFrameCacheDict = [NSMutableDictionary new];
for (NSString *key in _cacheDictionary.allKeys) {
NSArray *res = [key componentsSeparatedByString:@"-"];
long section = [res.firstObject integerValue];
long row = [res.lastObject integerValue];
if (section == sectionOfToDeleteItem && row > rowOfToDeleteItem) {
NSNumber *heightCache = _cacheDictionary[key];
NSArray *frameCache = _subviewFrameCacheDict[key];
NSString *newKey = [NSString stringWithFormat:@"%ld-%ld", section, (row - 1)];
[tempHeightCacheDict setValue:heightCache forKey:newKey];
[tempFrameCacheDict setValue:frameCache forKey:newKey];
[_cacheDictionary removeObjectForKey:key];
[_subviewFrameCacheDict removeObjectForKey:key];
}
}
[_cacheDictionary addEntriesFromDictionary:tempHeightCacheDict];
[_subviewFrameCacheDict addEntriesFromDictionary:tempFrameCacheDict];

}

- (NSNumber *)heightCacheForIndexPath:(NSIndexPath *)indexPath
{
/*
Expand Down Expand Up @@ -256,7 +286,7 @@ + (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{

NSArray *selStringsArray = @[@"reloadData", @"reloadRowsAtIndexPaths:withRowAnimation:"];
NSArray *selStringsArray = @[@"reloadData", @"reloadRowsAtIndexPaths:withRowAnimation:", @"deleteRowsAtIndexPaths:withRowAnimation:"];

[selStringsArray enumerateObjectsUsingBlock:^(NSString *selString, NSUInteger idx, BOOL *stop) {
NSString *mySelString = [@"sd_" stringByAppendingString:selString];
Expand All @@ -283,6 +313,14 @@ - (void)sd_reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITabl
[self sd_reloadRowsAtIndexPaths:indexPaths withRowAnimation:animation];
}

- (void)sd_deleteRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
{
for (NSIndexPath *indexPath in indexPaths) {
[self.cellAutoHeightManager deleteThenResetHeightCache:indexPath];
}
[self sd_deleteRowsAtIndexPaths:indexPaths withRowAnimation:animation];
}

/*
* 下一步即将实现的功能
Expand All @@ -291,11 +329,6 @@ - (void)sd_insertRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAn
[self sd_insertRowsAtIndexPaths:indexPaths withRowAnimation:animation];
}
- (void)sd_deleteRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
{
[self sd_deleteRowsAtIndexPaths:indexPaths withRowAnimation:animation];
}
- (void)sd_moveRowAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath
{
[self sd_moveRowAtIndexPath:indexPath toIndexPath:newIndexPath];
Expand Down

0 comments on commit 39aa83c

Please sign in to comment.