- Notifications
You must be signed in to change notification settings - Fork1
Auto layout UITableViewCell height calculate and cache.
License
Tinghui/UITableView-CellHeightCalculation
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
The basic idea is from this stackoverflow question:http://stackoverflow.com/questions/18746929/using-auto-layout-in-uitableview-for-dynamic-cell-layouts-variable-row-heights (in Chinese).
So this library is designed to simplifies the height calculation and cache for auto layout views. And we provide two components in this library:MFViewHeightCache andUITableView+CellHeightCalculation.
MFViewHeightCache provides a simple way to calculate and cache the height of any kinds of auto layout views.
It basically adds a category method on UIView to calculates view's height, and then builds a cache on top of this method.
- (CGFloat)mf_heightForWidth:(CGFloat)width withHeightCalculationView:(nullable UIView *)view configuration:(nullablevoid(^)(void))configuration {NSAssert(self.translatesAutoresizingMaskIntoConstraints,@"View must be auto layout enabled.");if (configuration !=nil) {configuration(); } [selfsetNeedsUpdateConstraints]; [selfupdateConstraintsIfNeeded]; [selfsetBounds:CGRectMake(0.0,0.0, width,CGRectGetHeight(self.bounds))]; [selfsetNeedsLayout]; [selflayoutIfNeeded];returnceil([(view ?:self)systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height);}
The basically usage is cache the view first, then call the height calcualtion methods. For example:
// Let's say you have a heightCache property in your view controller.@property (nonatomic, strong) MFViewHeightCache *heightCache;// Cache view first.- (MFViewHeightCache *)heightCache {if (_heightCache ==nil) { _heightCache = [[MFViewHeightCachealloc]init]; BookCell *cell = (BookCell *)[[[NSBundlemainBundle]loadNibNamed:NSStringFromClass([BookCellclass])owner:niloptions:nil]firstObject]; [_heightCachecacheView:cellwithKey:NSStringFromClass([BookCellclass])heightCalculatedByView:cell.contentView]; }return _heightCache;}// Then use heightCache to calculate and cache view's height.__weaktypeof(self) weakSelf = self;const CGFloat height = [self.heightCacheheightForViewWithKey:NSStringFromClass([BookCellclass])width:CGRectGetWidth(tableView.bounds)heightCacheKey:dataDict[@"ISBN"]viewConfiguration:^(__kindof UIView * _Nonnull view) { [weakSelf_configureCell:(BookCell *)viewwithDict:dataDict]; }] +1.0;
Or if you don't need the cache, you can just use one of these UIView's category methods to calculate view's height:
- (CGFloat)mf_heightForWidth:(CGFloat)width;- (CGFloat)mf_heightForWidth:(CGFloat)width configuration:(nullablevoid(^)(void))configuration;- (CGFloat)mf_heightForWidth:(CGFloat)width withHeightCalculationView:(nullable UIView *)view configuration:(nullablevoid(^)(void))configuration;
To integrate MFViewHeightCache into your Xcode project using CocoaPods, specify it in yourPodfile:
pod'MFViewHeightCache','~> 1.1.0'
UITableView+CellHeightCalculation is an UITableView category that builds on top of MFViewHeightCache.
The APIs are very simple, basically all you have to do is in UITableViewDelegate-tableView:heightForRowAtIndexPath: method:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {return [tableViewmf_heightForCellWithIdentifier:@"cell reuse identifer"cellConfiguration:^(__kindof UITableViewCell * _Nonnull cell) {//Configure cell with data, same as what you've done in "-tableView:cellForRowAtIndexPath:" }];}
To integrate UITableView+CellHeightCalculation into your Xcode project using CocoaPods, specify it in yourPodfile:
pod'UITableView+CellHeightCalculation','~> 1.1.1'
UITableView+CellHeightCalculation is distributed under anMIT License.
The MIT License (MIT)
Copyright (c) 2016 Tinghui
Permission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in allcopies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THESOFTWARE.
About
Auto layout UITableViewCell height calculate and cache.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.