|
| 1 | +// The MIT License (MIT) |
| 2 | +// |
| 3 | +// Copyright (c) 2015-1016 forkingdog ( https://github.com/forkingdog ) |
| 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 | +#import "UITableView+FDTemplateLayoutCell.h" |
| 24 | +#import <objc/runtime.h> |
| 25 | + |
| 26 | +@implementation UITableView (FDTemplateLayoutCell) |
| 27 | + |
| 28 | +- (id)fd_templateCellForReuseIdentifier:(NSString *)identifier; |
| 29 | +{ |
| 30 | + NSAssert(identifier.length > 0, @"Expect a valid identifier - %@", identifier); |
| 31 | + |
| 32 | + NSMutableDictionary *templateCellsByIdentifiers = objc_getAssociatedObject(self, _cmd); |
| 33 | + if (!templateCellsByIdentifiers) { |
| 34 | + templateCellsByIdentifiers = @{}.mutableCopy; |
| 35 | + objc_setAssociatedObject(self, _cmd, templateCellsByIdentifiers, OBJC_ASSOCIATION_RETAIN_NONATOMIC); |
| 36 | + } |
| 37 | + |
| 38 | + UITableViewCell *templateCell = templateCellsByIdentifiers[identifier]; |
| 39 | + if (!templateCell) { |
| 40 | + templateCell = [self dequeueReusableCellWithIdentifier:identifier]; |
| 41 | + templateCellsByIdentifiers[identifier] = templateCell; |
| 42 | + } |
| 43 | + |
| 44 | + return templateCell; |
| 45 | +} |
| 46 | + |
| 47 | +- (CGFloat)fd_heightForCellWithIdentifier:(NSString *)identifier configuration:(void (^)(id))configuration |
| 48 | +{ |
| 49 | + // Fetch a cached template cell for indentifier |
| 50 | + UITableViewCell *cell = [self fd_templateCellForReuseIdentifier:identifier]; |
| 51 | + |
| 52 | + // Reset to default |
| 53 | + cell.contentView.bounds = CGRectMake(0, 0, CGRectGetWidth(self.frame), self.rowHeight); |
| 54 | + |
| 55 | + // Keep what real cells do |
| 56 | + [cell prepareForReuse]; |
| 57 | + |
| 58 | + // Configure our template cell |
| 59 | + if (configuration) { |
| 60 | + configuration(cell); |
| 61 | + } |
| 62 | + |
| 63 | + // TODO: What's this fucking temp constraint? |
| 64 | + NSLayoutConstraint *tempWidthConstraint = |
| 65 | + [NSLayoutConstraint constraintWithItem:cell.contentView |
| 66 | + attribute:NSLayoutAttributeWidth |
| 67 | + relatedBy:NSLayoutRelationEqual |
| 68 | + toItem:nil |
| 69 | + attribute:NSLayoutAttributeNotAnAttribute |
| 70 | + multiplier:1.0 |
| 71 | + constant:CGRectGetWidth(self.frame)]; |
| 72 | + [cell.contentView addConstraint:tempWidthConstraint]; |
| 73 | + |
| 74 | + // Auto layout system does its math |
| 75 | + CGSize fittingSize = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]; |
| 76 | + |
| 77 | + [cell.contentView removeConstraint:tempWidthConstraint]; |
| 78 | + |
| 79 | + // Add 1px for separator line if needed, screen scale respected |
| 80 | + if (self.separatorStyle != UITableViewCellSeparatorStyleNone) { |
| 81 | + fittingSize.height += 1.0 / [UIScreen mainScreen].scale; |
| 82 | + } |
| 83 | + |
| 84 | + return fittingSize.height; |
| 85 | +} |
| 86 | + |
| 87 | +@end |
0 commit comments