Skip to content

Commit 05ea157

Browse files
committed
Initial demo project
1 parent f2b6827 commit 05ea157

32 files changed

+1171
-0
lines changed

Classes/FDTextFeedCell.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// FDTextFeedCell.h
3+
// Demo
4+
//
5+
// Created by sunnyxx on 15/4/16.
6+
// Copyright (c) 2015年 forkingdog. All rights reserved.
7+
//
8+
9+
#import <UIKit/UIKit.h>
10+
11+
@interface FDTextFeedCell : UITableViewCell
12+
13+
@end
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 <UIKit/UIKit.h>
24+
25+
@interface UITableView (FDTemplateLayoutCell)
26+
27+
- (CGFloat)fd_heightForCellWithIdentifier:(NSString *)identifier configuration:(void (^)(id cell))configuration;
28+
29+
@end
30+
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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

Comments
 (0)