diff --git a/SDAutoLayoutDemo.xcodeproj/project.xcworkspace/xcuserdata/gsd.xcuserdatad/UserInterfaceState.xcuserstate b/SDAutoLayoutDemo.xcodeproj/project.xcworkspace/xcuserdata/gsd.xcuserdatad/UserInterfaceState.xcuserstate index 9882a31..62f895b 100644 Binary files a/SDAutoLayoutDemo.xcodeproj/project.xcworkspace/xcuserdata/gsd.xcuserdatad/UserInterfaceState.xcuserstate and b/SDAutoLayoutDemo.xcodeproj/project.xcworkspace/xcuserdata/gsd.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/SDAutoLayoutDemo/DemoVC/DemoVC5/DemoVC5.m b/SDAutoLayoutDemo/DemoVC/DemoVC5/DemoVC5.m index 19b7375..615b51e 100644 --- a/SDAutoLayoutDemo/DemoVC/DemoVC5/DemoVC5.m +++ b/SDAutoLayoutDemo/DemoVC/DemoVC5/DemoVC5.m @@ -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 * >>>>>>>>>>>>>>>>>>>>>>>> diff --git a/SDAutoLayoutDemo/SDAutoLayout/UITableView+SDAutoTableViewCellHeight.h b/SDAutoLayoutDemo/SDAutoLayout/UITableView+SDAutoTableViewCellHeight.h index de1e96b..4a77027 100644 --- a/SDAutoLayoutDemo/SDAutoLayout/UITableView+SDAutoTableViewCellHeight.h +++ b/SDAutoLayoutDemo/SDAutoLayout/UITableView+SDAutoTableViewCellHeight.h @@ -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; diff --git a/SDAutoLayoutDemo/SDAutoLayout/UITableView+SDAutoTableViewCellHeight.m b/SDAutoLayoutDemo/SDAutoLayout/UITableView+SDAutoTableViewCellHeight.m index c03a419..ea173f8 100644 --- a/SDAutoLayoutDemo/SDAutoLayout/UITableView+SDAutoTableViewCellHeight.m +++ b/SDAutoLayoutDemo/SDAutoLayout/UITableView+SDAutoTableViewCellHeight.m @@ -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 { /* @@ -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]; @@ -283,6 +313,14 @@ - (void)sd_reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITabl [self sd_reloadRowsAtIndexPaths:indexPaths withRowAnimation:animation]; } +- (void)sd_deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation +{ + for (NSIndexPath *indexPath in indexPaths) { + [self.cellAutoHeightManager deleteThenResetHeightCache:indexPath]; + } + [self sd_deleteRowsAtIndexPaths:indexPaths withRowAnimation:animation]; +} + /* * 下一步即将实现的功能 @@ -291,11 +329,6 @@ - (void)sd_insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAn [self sd_insertRowsAtIndexPaths:indexPaths withRowAnimation:animation]; } - - (void)sd_deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation - { - [self sd_deleteRowsAtIndexPaths:indexPaths withRowAnimation:animation]; - } - - (void)sd_moveRowAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath { [self sd_moveRowAtIndexPath:indexPath toIndexPath:newIndexPath];