I am using UITableViewController
instead detailView to show one entity details. I populated one row of data from PFQuery in my viewDidLoad
method.
If you are using autolayout. Add top, left and right constrain in to your label.
Then in heightForRowAtIndexPath
create your cell
static NSString *simpleTableIdentifier = @"DetailsDesecriptionCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
Set values to label and calculate size of the label after setting the value
[cell layoutIfNeeded];
return cell.label.frame.origin.y+cell.label.frame.size.height;
This will give you the exact hight of the label and your cell height will be same
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *simpleTableIdentifier = @"DetailsDesecriptionCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
cell.titleLabel.text=@"YOUR TEXT";
[cell layoutIfNeeded];
return cell.titleLabel.frame.origin.y+cell.titleLabel.frame.size.height;
}
If it is IOS 8 OR 9, there is a simple fix.
tableView.estimatedRowHeight = 68.0 tableView.rowHeight = UITableViewAutomaticDimension
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { return UITableViewAutomaticDimension }
I have been doing trying to achieve this from many days but could not. I tried Fahim answer and got clue, following I got from other different answer and have been able to complete it. Code in I added following function to get exact size of my label
-(CGFloat)getLabelHeightForText:(NSString *)text andWidth:(CGFloat)labelWidth
{
CGSize maximumSize = CGSizeMake(labelWidth, 10000);
NSLog(@"crossing getLabelheightForText");
//provide appropriate font and font size
CGSize labelHeighSize = [text sizeWithFont: [UIFont fontWithName:@"Trebuchet MS" size:17.0f]
constrainedToSize:maximumSize
lineBreakMode:NSLineBreakByTruncatingTail];
return labelHeighSize.height;
}
Than I have added indexPath with all cell sizes to be returned showing here only one cell size.
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
switch (indexPath.section)
{
case 0:
{
if (indexPath.row == 0) // category name size
return 44.0;
if (indexPath.row == 1) // image size
return 204;
}
default:
return 44.0
}
}
But after this I was getting perfect size of label but still have more to do as I was using PFQuery and tableView indexPath runs first that is why it could not update my table and I was still not able to resize cell. Than here is magic line that reloads my tableView and resolve my issue
[self.tableView reloadData];
Although issue is simple but it has a lot things so I put as reply to help other peoples.
Below is how I do.
// this will set height of the row
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
String *yourLongString = "Long text here";
UILabel *mLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0,320,30)];
your size will change here
mLabel.hidden = YES;
mLabel.text = yourLongString;
[mLabel sizeToFit];
^^^^^^^^ this is very important
return mLabel.frame.size.height;
}
Now in cellForRowAtIndexPath
adjust the height of actual label as per the height of the cell.
Let me know if you are not clear.