Many application has dynamic contents to show from the server. Also many applications has a design to show some controls/views when the user selects the cell to expand.
So, we need to calculate the tableview cell height dynamic and assign to the UITableView Datasource and Delegates.
1. Calculate Table Cell Height
CGFloat height = 5.0f;
// We are calculating the height based on the value..
if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@”6.0″)) {
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
height += [dynamicString boundingRectWithSize:CGSizeMake([[UIScreen mainScreen] bounds].size.width – 20, 9999) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont fontWithName:@”Helvetica” size:16], NSParagraphStyleAttributeName:paragraphStyle} context:nil].size.height;
}
else {
height += ceilf([dynamicString sizeWithFont:[UIFont fontWithName:@”Helvetica” size:16] constrainedToSize:CGSizeMake([[UIScreen mainScreen] bounds].size.width – 20, 9999) lineBreakMode:NSLineBreakByWordWrapping].height);
}
Here is a sample view controller with Tableview integrated..
#import “ViewController.h”
#import “DemoTableViewCell.h”
@interface ViewController ()
@property (nonatomic, unsafe_unretained) IBOutlet UITableView *demoTV;
@property (nonatomic, strong) NSMutableArray *heightArray;
@property (assign) BOOL isNeedToExpand;
@end
static NSString *demoTableCellID = @”DemoTableViewCellIdentifier”;
#define IS_IPHONE5 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height – ( double )568 ) < DBL_EPSILON )
#define SYSTEM_VERSION_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)
@implementation ViewController
– (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.title = @”Home”;
self.isNeedToExpand = NO;
}
#pragma mark- Webservice Call
– (void) fetchDataFromServer {
// Storing the dynamic height in an array…
CGFloat height = 0;
CGFloat captionHeight = [ViewController calculateDynamicCellHeight:@”Value from server”];
height = height + captionHeight + 10;
[self.heightArray addObject:[NSString stringWithFormat:@”%f”,captionHeight]];
}
#pragma mark- UITableview Delegate
– (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
– (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 4;
}
– (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
// Here you can handle the dynamic height from an heightArray values..
if (self.isNeedToExpand) {
if ((indexPath.row == 2) || (indexPath.row == 1)) {
return 110;
} else {
return 50;
}
}
return 50;
}
– (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
DemoTableViewCell *cell = (DemoTableViewCell *) [tableView dequeueReusableCellWithIdentifier:demoTableCellID forIndexPath:indexPath];
if ((indexPath.row == 2) || (indexPath.row == 1)) {
cell.hiddenLabel.hidden = NO;
} else {
cell.hiddenLabel.hidden = YES;
}
return cell;
}
– (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Here you have to handle the indexPath which needs to be expand based on the height..
[self.demoTV beginUpdates];
if ((indexPath.row == 2) || (indexPath.row == 1)) {
self.isNeedToExpand = YES;
} else {
self.isNeedToExpand = NO;
}
[self.demoTV endUpdates];
}
// We are calculating the dynamic cell height based on the string…
#pragma mark- Calcualte Cell Height
+ (CGFloat) calculateDynamicCellHeight: (NSString *) dynamicString {
CGFloat height = 5.0f;
// We are calculating the height based on the value..
if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@”6.0″)) {
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
height += [dynamicString boundingRectWithSize:CGSizeMake([[UIScreen mainScreen] bounds].size.width – 20, 9999) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont fontWithName:@”Helvetica” size:16], NSParagraphStyleAttributeName:paragraphStyle} context:nil].size.height;
}
else {
height += ceilf([dynamicString sizeWithFont:[UIFont fontWithName:@”Helvetica” size:16] constrainedToSize:CGSizeMake([[UIScreen mainScreen] bounds].size.width – 20, 9999) lineBreakMode:NSLineBreakByWordWrapping].height);
}
return height;
}
– (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
The above source code is just for an idea about dynamic height and expanding cells. Ourself we have to manage the cell views as per our requirement.
Hope it will help someone. Happy Coding. Thanks.