I am making one simple map based application. There are two labels in the custom cell of table view. One of label's text is dynamic and I want to change the cell's height according to label's text and height.
Asked
Active
Viewed 1,668 times
0
-
4Possible duplicate of [Using Auto Layout in UITableView for dynamic cell layouts & variable row heights](http://stackoverflow.com/questions/18746929/using-auto-layout-in-uitableview-for-dynamic-cell-layouts-variable-row-heights) – Feb 20 '16 at 06:40
3 Answers
0
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
NSMutableString *strauditType = @"Your String ";
CGRect textRect = [strauditType boundingRectWithSize:maximumLabelSize
options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14.0]}
context:nil];
CGSize messageSize = textRect.size;
return messageSize.height + 10.0f;
}

Yagnesh Dobariya
- 2,241
- 19
- 29
0
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewAutomaticDimension;
}

Sabby
- 403
- 3
- 15
0
I tried and got the solution simply.
ViewController.m
#import "ViewController.h"
#import "CustomTableViewCell.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
NSMutableArray *array;
}
@property (strong, nonatomic) IBOutlet UITableView *tableViewIncreaseRowHeight;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIFont *labelFont = [UIFont fontWithName:@"Noteworthy-Bold" size:20];
NSDictionary *arialdict = [NSDictionary dictionaryWithObject:labelFont forKey:NSFontAttributeName];
NSMutableAttributedString *message = [[NSMutableAttributedString alloc] initWithString:@"this is just the sample example of how to calculate the dynamic height for tableview cell which is of around 7 to 8 lines. you will need to set the height of this string first, not seems to be calculated in cellForRowAtIndexPath method." attributes:arialdict];
array = [NSMutableArray arrayWithObjects:message, nil];
NSMutableAttributedString *message_1 = [[NSMutableAttributedString alloc] initWithString:@"you will need to set the height of this string first, not seems to be calculated in cellForRowAtIndexPath method." attributes:arialdict];
[array addObject:message_1];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//table view delegate and data source methods
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableHeaderFooterViewWithIdentifier:@"cell"];
NSArray *arr = [[NSBundle mainBundle]loadNibNamed:@"CustomTableViewCell" owner:self options:nil];
if(cell==nil)
{
cell = arr[0];
}
cell.labelName.attributedText = array[indexPath.row] ;
cell.labelName.numberOfLines = 2;
[cell.labelName sizeToFit];
cell.labelID.text = @"20";
return cell;
}
-(float)height :(NSMutableAttributedString*)string
{
NSAttributedString *attributedText = string;
//you need to specify the some width, height will be calculated
CGRect rect = [attributedText boundingRectWithSize:(CGSize){225, MAXFLOAT}
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
CGSize requiredSize = rect.size;
//finally u return your height
return requiredSize.height;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//whatever the height u need to calculate calculate hear only
CGFloat heightOfcell = [self height:array[indexPath.row]];
NSLog(@"%f",heightOfcell);
return heightOfcell;
}
@end
Credits go to Shan
Thank you so much:-)

Community
- 1
- 1

user3182143
- 9,459
- 3
- 32
- 39