Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Auto layout UITableViewCell height calculate and cache.

License

NotificationsYou must be signed in to change notification settings

Tinghui/UITableView-CellHeightCalculation

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

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);}

Basically useage

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;

Installation by CocoaPods

To integrate MFViewHeightCache into your Xcode project using CocoaPods, specify it in yourPodfile:

pod'MFViewHeightCache','~> 1.1.0'

UITableView+CellHeightCalculation

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:"    }];}

Installation by CocoaPods

To integrate UITableView+CellHeightCalculation into your Xcode project using CocoaPods, specify it in yourPodfile:

pod'UITableView+CellHeightCalculation','~> 1.1.1'

License

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

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp