Skip to content

Commit 34b6fc2

Browse files
author
Ernesto Rivera
committed
heightForCellWithIdentifier -> heightForResizableCellWithIdentifier and support UITextView
1 parent 2d03377 commit 34b6fc2

File tree

3 files changed

+54
-35
lines changed

3 files changed

+54
-35
lines changed

Demo/TableDemo/PEPhotosDetailViewController.m

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,9 @@ - (AMBTableViewSection *)commentsSection
167167
}
168168

169169
// Dynamic height comments
170-
return [weakSelf heightForCellWithIdentifier:@"comment"
171-
text:object
172-
limitedToNumberOfLines:0];
170+
return [weakSelf heightForResizableCellWithIdentifier:@"comment"
171+
text:object
172+
limitedToNumberOfLines:0];
173173
}
174174
cellIdentifierBlock:^NSString *(id object,
175175
NSIndexPath * indexPath)
@@ -193,7 +193,7 @@ - (AMBTableViewSection *)commentsSection
193193
{
194194
return @"Comments";
195195
};
196-
196+
197197
// Enable "no content cell"
198198
_commentsSection.presentsNoContentCell = YES;
199199
}
@@ -336,7 +336,7 @@ @implementation PEPhotosDetailAuthorCell
336336

337337
@implementation PEPhotosDetailCommentCell
338338

339-
@synthesize resizableLabel = _resizableLabel;
339+
@synthesize resizableView = _resizableView;
340340

341341
@end
342342

Source/AMBTableViewController.h

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,18 @@
110110
/// @param subview A subview of the row.
111111
- (NSIndexPath *)indexPathForRowWithSubview:(UIView *)subview;
112112

113-
/// Calculate the required height for a dynamic height label.
113+
/// Calculate the required height for a AMBResizableCell.
114114
/// @param identifier The identifier of the AMBResizableCell who's height will be calculated.
115-
/// @param text The text that the [AMBResizableCell resizableLabel] should fit.
116-
/// @param numberOfLines The maximum number of lines to be used by the label or `0` for no limits.
115+
/// @param text The text that the view should fit.
116+
/// @param numberOfLines The maximum number of lines to be allowed or `0` for no limits.
117+
/// Only implemented for UILabel resizableView's.
117118
/// @discussion A cell instance of the given identifier will be cached to calculate the heights
118119
/// of all future calculations. The original height of the loaded cell will be used as the
119120
/// minimum height to be returned by the function.
120-
- (CGFloat)heightForCellWithIdentifier:(NSString *)identifier
121-
text:(NSString *)text
122-
limitedToNumberOfLines:(NSInteger)numberOfLines;
121+
/// @note Currently only suport for UILabel and UITextView objects is implemented.
122+
- (CGFloat)heightForResizableCellWithIdentifier:(NSString *)identifier
123+
text:(NSString *)text
124+
limitedToNumberOfLines:(NSInteger)numberOfLines;
123125

124126
@end
125127

@@ -397,17 +399,18 @@ typedef void (^AMBTableViewCellConfigurationBlock)(id object,
397399

398400
/**
399401
A protocol that when implemented by a UITableViewCell enables using
400-
[AMBTableViewController heightForCellWithIdentifier:text:limitedToNumberOfLines:]
402+
[AMBTableViewController heightForResizableCellWithIdentifier:text:limitedToNumberOfLines:]
401403
to easily calculate dynamic-sized cells.
404+
@note Currently only suport for UILabel and UITextView objects is implemented.
402405
*/
403406
@protocol AMBResizableCell <NSObject>
404407

405408
/// The label that determines the required height of the cell.
406-
/// @discussion The original cell height is saved and used as the minimum height to used
407-
/// for the cell.
408-
/// @note The label should be set to use flexible height and automatically grow/shrink
409-
/// when its parent cell changes adjusts its height.
410-
@property (weak, nonatomic) IBOutlet UILabel * resizableLabel;
409+
/// @discussion The original cell height is saved and used as the minimum height
410+
/// to be used for the cell.
411+
/// @note The label should be set to use flexible height and automatically
412+
/// grow/shrink when its parent cell adjusts its height.
413+
@property (weak, nonatomic) IBOutlet UIView * resizableView;
411414

412415
@end
413416

Source/AMBTableViewController.m

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,9 @@ - (NSIndexPath *)indexPathForRowWithSubview:(UIView *)subview
197197
return [self.tableView indexPathForRowAtPoint:point];
198198
}
199199

200-
- (CGFloat)heightForCellWithIdentifier:(NSString *)identifier
201-
text:(NSString *)text
202-
limitedToNumberOfLines:(NSInteger)numberOfLines
200+
- (CGFloat)heightForResizableCellWithIdentifier:(NSString *)identifier
201+
text:(NSString *)text
202+
limitedToNumberOfLines:(NSInteger)numberOfLines
203203
{
204204
static NSMutableDictionary * cache;
205205
static dispatch_once_t onceToken;
@@ -212,7 +212,7 @@ - (CGFloat)heightForCellWithIdentifier:(NSString *)identifier
212212
NSDictionary * cachedValues = cache[uniqueCellIdenfier];
213213
CGFloat minimumHeight;
214214
CGSize sizeDifference;
215-
UILabel * label;
215+
UIView * resizableView;
216216

217217
if (!cachedValues)
218218
{
@@ -221,35 +221,51 @@ - (CGFloat)heightForCellWithIdentifier:(NSString *)identifier
221221
NSAssert([cell conformsToProtocol:@protocol(AMBResizableCell)], @"Cell doesn't conform to the AMBResizableCell protocol.");
222222

223223
minimumHeight = cell.frame.size.height;
224-
label = cell.resizableLabel;
224+
resizableView = cell.resizableView;
225225
sizeDifference = cell.frame.size;
226-
sizeDifference.width -= label.frame.size.width;
227-
sizeDifference.height -= label.frame.size.height;
226+
sizeDifference.width -= resizableView.frame.size.width;
227+
sizeDifference.height -= resizableView.frame.size.height;
228228

229-
NSAssert(label, @"No resizableLabel set in cell");
229+
NSAssert(resizableView, @"No resizableView set in cell");
230230

231231
cachedValues = @{@"minimumHeight" : @(minimumHeight),
232232
@"sizeDifference" : [NSValue valueWithCGSize:sizeDifference],
233-
@"label" : label};
233+
@"resizableView" : resizableView};
234234
cache[uniqueCellIdenfier] = cachedValues;
235235
}
236236
else
237237
{
238238
minimumHeight = ((NSNumber *)cachedValues[@"minimumHeight"]).floatValue;
239239
sizeDifference = ((NSNumber *)cachedValues[@"sizeDifference"]).CGSizeValue;
240-
label = cachedValues[@"label"];
240+
resizableView = cachedValues[@"resizableView"];
241241
}
242242

243-
label.text = text;
244-
CGRect labelBounds = CGRectMake(0.0,
245-
0.0,
246-
self.tableView.frame.size.width - sizeDifference.width,
247-
CGFLOAT_MAX);
248-
CGRect rect = [label textRectForBounds:labelBounds
249-
limitedToNumberOfLines:numberOfLines];
243+
CGFloat heightThatFits = 0.0;
244+
CGRect frame = CGRectMake(0.0,
245+
0.0,
246+
self.tableView.frame.size.width - sizeDifference.width,
247+
CGFLOAT_MAX);
248+
if ([resizableView isKindOfClass:[UILabel class]])
249+
{
250+
UILabel * resizableLabel = (UILabel *)resizableView;
251+
resizableLabel.text = text;
252+
heightThatFits = [resizableLabel textRectForBounds:frame
253+
limitedToNumberOfLines:numberOfLines].size.height;
254+
}
255+
else if ([resizableView isKindOfClass:[UITextView class]])
256+
{
257+
UITextView * resizableTextView = (UITextView *)resizableView;
258+
resizableTextView.text = text;
259+
heightThatFits = [resizableTextView sizeThatFits:frame.size].height;
260+
// TODO: Adjust to numberOfLines
261+
}
262+
else
263+
{
264+
NSAssert(false, @"The provided resizableView class '%@' is not supported", NSStringFromClass([resizableView class]));
265+
}
250266

251267
CGFloat cellSeparatorHeight = (self.tableView.separatorStyle == UITableViewCellSeparatorStyleNone) ? 0.0 : 1.0;
252-
return MAX(rect.size.height + sizeDifference.height + cellSeparatorHeight,
268+
return MAX(heightThatFits + sizeDifference.height + cellSeparatorHeight,
253269
minimumHeight);
254270
}
255271

0 commit comments

Comments
 (0)