├── LICENSE ├── README.md ├── UITableViewController+KYSelfSizingPushFix.podspec ├── UITableViewController+KYSelfSizingPushFix ├── UITableView+KYSelfSizingPushFix.h └── UITableView+KYSelfSizingPushFix.m └── logo.png /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 KittenYang 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | KYSelfSizingPushFixCategory 3 |

4 | 5 | 6 | ![CocoaPods Version](https://img.shields.io/badge/pod-v1.0.0-brightgreen.svg) 7 | ![License](https://img.shields.io/badge/license-MIT-blue.svg) 8 | ![Platform](https://img.shields.io/badge/platform-iOS-red.svg) 9 | 10 | 11 | 12 | A UITableView Category for fixing the bug of 'Self-Sizing-Cell' when push to next ViewController the tableView position will change. 13 | 14 | 15 | #Installation 16 | 17 | `pod 'UITableViewController+KYSelfSizingPushFix', '~> 1.0.0'` 18 | 19 | 20 | ##How to use 21 | 22 | ###Three Steps: 23 | 24 | **1.Add code in `- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath`** 25 | 26 | ```objective-c 27 | - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 28 | 29 | ... 30 | 31 | if (![self ky_isEstimatedRowHeightInCache:indexPath]) { 32 | CGSize cellSize = [cell systemLayoutSizeFittingSize:CGSizeMake(self.view.frame.size.width, 0) withHorizontalFittingPriority:1000.0 verticalFittingPriority:50.0]; 33 | [self ky_putEstimatedCellHeightToCache:indexPath height:cellSize.height]; 34 | } 35 | 36 | ... 37 | 38 | } 39 | ``` 40 | 41 | **2.Implement `estimatedHeightForRowAtIndexPath:`:** 42 | 43 | ```objective-c 44 | -(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath { 45 | return [self ky_getEstimatedCellHeightFromCache:indexPath defaultHeight:250.0f]; 46 | } 47 | ``` 48 | 49 | 50 | **3.Remember to use` [self ky_tableViewReloadData];` rather than `[self.tableView reloadData];`** 51 | 52 | 53 | 54 | ##*You're done!* 55 | -------------------------------------------------------------------------------- /UITableViewController+KYSelfSizingPushFix.podspec: -------------------------------------------------------------------------------- 1 | # 2 | # Be sure to run `pod spec lint UITableViewController+KYSelfSizingPushFix.podspec' to ensure this is a 3 | # valid spec and to remove all comments including this before submitting the spec. 4 | # 5 | # To learn more about Podspec attributes see http://docs.cocoapods.org/specification.html 6 | # To see working Podspecs in the CocoaPods repo see https://github.com/CocoaPods/Specs/ 7 | # 8 | 9 | Pod::Spec.new do |s| 10 | 11 | # ――― Spec Metadata ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― # 12 | # 13 | # These will help people to find your library, and whilst it 14 | # can feel like a chore to fill in it's definitely to your advantage. The 15 | # summary should be tweet-length, and the description more in depth. 16 | # 17 | 18 | s.name = "UITableViewController+KYSelfSizingPushFix" 19 | s.version = "1.0.0" 20 | s.summary = "解决了iOS8中使用Self-Sizing Cell自动布局时,push时发生cell跳跃的bug" 21 | 22 | s.description = <<-DESC 23 | 给UITableViewController增加了一个Category。解决了iOS8中使用Self-Sizing Cell自动布局时,push时发生cell跳跃的bug 24 | DESC 25 | 26 | s.homepage = "https://github.com/KittenYang/KYSelfSizingPushFixCategory" 27 | # s.screenshots = "www.example.com/screenshots_1.gif", "www.example.com/screenshots_2.gif" 28 | 29 | 30 | # ――― Spec License ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― # 31 | # 32 | # Licensing your code is important. See http://choosealicense.com for more info. 33 | # CocoaPods will detect a license file if there is a named LICENSE* 34 | # Popular ones are 'MIT', 'BSD' and 'Apache License, Version 2.0'. 35 | # 36 | 37 | s.license = "MIT" 38 | # s.license = { :type => "MIT", :file => "FILE_LICENSE" } 39 | 40 | 41 | # ――― Author Metadata ――――――――――――――――――――――――――――――――――――――――――――――――――――――――― # 42 | # 43 | # Specify the authors of the library, with email addresses. Email addresses 44 | # of the authors are extracted from the SCM log. E.g. $ git log. CocoaPods also 45 | # accepts just a name if you'd rather not provide an email address. 46 | # 47 | # Specify a social_media_url where others can refer to, for example a twitter 48 | # profile URL. 49 | # 50 | 51 | s.author = { "KittenYang" => "kittenyang@icloud.com" } 52 | # Or just: s.author = "KittenYang" 53 | # s.authors = { "KittenYang" => "kittenyang@icloud.com" } 54 | # s.social_media_url = "http://twitter.com/KittenYang" 55 | 56 | # ――― Platform Specifics ――――――――――――――――――――――――――――――――――――――――――――――――――――――― # 57 | # 58 | # If this Pod runs only on iOS or OS X, then specify the platform and 59 | # the deployment target. You can optionally include the target after the platform. 60 | # 61 | 62 | s.platform = :ios 63 | # s.platform = :ios, "5.0" 64 | 65 | # When using multiple platforms 66 | # s.ios.deployment_target = "5.0" 67 | # s.osx.deployment_target = "10.7" 68 | 69 | 70 | # ――― Source Location ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― # 71 | # 72 | # Specify the location from where the source should be retrieved. 73 | # Supports git, hg, bzr, svn and HTTP. 74 | # 75 | 76 | s.source = { :git => "https://github.com/KittenYang/KYSelfSizingPushFixCategory.git", :tag => s.version.to_s } 77 | 78 | 79 | # ――― Source Code ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― # 80 | # 81 | # CocoaPods is smart about how it includes source code. For source files 82 | # giving a folder will include any swift, h, m, mm, c & cpp files. 83 | # For header files it will include any header in the folder. 84 | # Not including the public_header_files will make all headers public. 85 | # 86 | 87 | s.source_files = "UITableViewController+KYSelfSizingPushFix/*.{h,m}" 88 | #s.exclude_files = "Classes/Exclude" 89 | 90 | # s.public_header_files = "Classes/**/*.h" 91 | 92 | 93 | # ――― Resources ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― # 94 | # 95 | # A list of resources included with the Pod. These are copied into the 96 | # target bundle with a build phase script. Anything else will be cleaned. 97 | # You can preserve files from being cleaned, please don't preserve 98 | # non-essential files like tests, examples and documentation. 99 | # 100 | 101 | # s.resource = "icon.png" 102 | # s.resources = "Resources/*.png" 103 | 104 | # s.preserve_paths = "FilesToSave", "MoreFilesToSave" 105 | 106 | 107 | # ――― Project Linking ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― # 108 | # 109 | # Link your library with frameworks, or libraries. Libraries do not include 110 | # the lib prefix of their name. 111 | # 112 | 113 | s.framework = "Foundation","UIKit" 114 | # s.frameworks = "SomeFramework", "AnotherFramework" 115 | 116 | # s.library = "iconv" 117 | # s.libraries = "iconv", "xml2" 118 | 119 | 120 | # ――― Project Settings ――――――――――――――――――――――――――――――――――――――――――――――――――――――――― # 121 | # 122 | # If your library depends on compiler flags you can set them in the xcconfig hash 123 | # where they will only apply to your library. If you depend on other Podspecs 124 | # you can include multiple dependencies to ensure it works. 125 | 126 | s.requires_arc = true 127 | 128 | # s.xcconfig = { "HEADER_SEARCH_PATHS" => "$(SDKROOT)/usr/include/libxml2" } 129 | # s.dependency "JSONKit", "~> 1.4" 130 | 131 | end 132 | -------------------------------------------------------------------------------- /UITableViewController+KYSelfSizingPushFix/UITableView+KYSelfSizingPushFix.h: -------------------------------------------------------------------------------- 1 | // 2 | // UITableView+SelfSizingPushFix.h 3 | // 4 | // Created by Kitten Yang on 4/20/15. 5 | // Copyright (c) 2015 Kitten Yang. All rights reserved. 6 | // 7 | 8 | #import 9 | 10 | @interface UITableView (KYSelfSizingPushFix) 11 | 12 | /** 13 | * 第一次,把算出的高度存入字典,key为section-row 14 | * 15 | * @param indexPath 当前indexPath 16 | * @param height 存入的高度 17 | */ 18 | - (void) ky_putEstimatedCellHeightToCache:(NSIndexPath *) indexPath height:(CGFloat) height; 19 | 20 | /** 21 | * 取出当前indexPath的高度 22 | * 23 | * @param indexPath 当前indexPath 24 | * @param defaultHeight 默认高度 25 | * 26 | * @return 取出的高度 27 | */ 28 | - (CGFloat) ky_getEstimatedCellHeightFromCache:(NSIndexPath *) indexPath defaultHeight:(CGFloat) defaultHeight; 29 | 30 | /** 31 | * 判断字典中是否存在当前indexPath对应的高度 32 | * 33 | * @param indexPath 当前的indexPath 34 | * 35 | * @return 是否存在 36 | */ 37 | - (BOOL) ky_isEstimatedRowHeightInCache:(NSIndexPath *) indexPath; 38 | 39 | /** 40 | * 请用这个方法来刷新tableView取代reloadData 41 | */ 42 | - (void) ky_tableViewReloadData; 43 | 44 | @end 45 | -------------------------------------------------------------------------------- /UITableViewController+KYSelfSizingPushFix/UITableView+KYSelfSizingPushFix.m: -------------------------------------------------------------------------------- 1 | // 2 | // UITableViewController+SelfSizingPushFix.m 3 | // 4 | // Created by Kitten Yang on 4/20/15. 5 | // Copyright (c) 2015 Kitten Yang. All rights reserved. 6 | // 7 | 8 | #import "UITableView+KYSelfSizingPushFix.h" 9 | #import 10 | 11 | @implementation UITableView (SelfSizingPushFix) 12 | 13 | #pragma mark - 添加变量 estimatedRowHeightCache 14 | 15 | - (NSMutableDictionary *) estimatedRowHeightCache { 16 | 17 | NSMutableDictionary *cache = objc_getAssociatedObject(self, @selector(estimatedRowHeightCache)); 18 | if (cache == nil) { 19 | cache = [NSMutableDictionary new]; 20 | objc_setAssociatedObject(self, @selector(estimatedRowHeightCache), cache, OBJC_ASSOCIATION_RETAIN_NONATOMIC); 21 | } 22 | return cache; 23 | } 24 | 25 | 26 | 27 | #pragma mark - 具体实现的方法 28 | 29 | - (void) ky_putEstimatedCellHeightToCache:(NSIndexPath *) indexPath height:(CGFloat) height { 30 | [self.estimatedRowHeightCache setObject:@(height) forKey:[self cacheKeyForIndexPath:indexPath]]; 31 | } 32 | 33 | - (CGFloat) ky_getEstimatedCellHeightFromCache:(NSIndexPath *) indexPath defaultHeight:(CGFloat) defaultHeight { 34 | NSNumber *estimatedHeight = [self.estimatedRowHeightCache objectForKey:[self cacheKeyForIndexPath:indexPath]]; 35 | if (estimatedHeight != nil) { 36 | return [estimatedHeight floatValue]; 37 | } 38 | return defaultHeight; 39 | } 40 | 41 | - (BOOL) ky_isEstimatedRowHeightInCache:(NSIndexPath *) indexPath { 42 | if ([self.estimatedRowHeightCache objectForKey:[self cacheKeyForIndexPath:indexPath]] != nil) { 43 | return YES; 44 | } 45 | return NO; 46 | } 47 | 48 | -(void) clearEstimatedRowCacheForIndexPath:(NSIndexPath *) indexPath { 49 | [self.estimatedRowHeightCache removeObjectForKey:[self cacheKeyForIndexPath:indexPath]]; 50 | } 51 | 52 | - (void) clearAllEstimatedRowCache { 53 | [self.estimatedRowHeightCache removeAllObjects]; 54 | } 55 | 56 | 57 | - (void) ky_tableViewReloadData{ 58 | [self clearAllEstimatedRowCache]; 59 | [self.tableView reloadData]; 60 | } 61 | 62 | #pragma mark - Helpers 63 | 64 | - (NSString *)cacheKeyForIndexPath:(NSIndexPath *)indexPath { 65 | return [NSString stringWithFormat:@"%ld-%ld", (long) indexPath.section, (long) indexPath.row]; 66 | } 67 | 68 | @end 69 | 70 | -------------------------------------------------------------------------------- /logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KittenYang/KYSelfSizingPushFixCategory/c0120bee9e7e0677ff63fa68cef048e98bc2066d/logo.png --------------------------------------------------------------------------------