Dynamic Height for Static Table Cells with Wrapping Labels

Dynamic height for static table cells with wrapping labels?

Use UITableView's heightForRowAtIndexPath :

 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
int topPadding = 10;
int bottomPadding = 10;
float landscapeWidth = 400;
float portraitWidth = 300;

UIFont *font = [UIFont fontWithName:@"Arial" size:22];

//This is for first cell only if you want for all then remove below condition
if (indexPath.row == 0) // for cell with dynamic height
{
NSString *strText = [[arrTexts objectAtIndex:indexPath.row]; // filling text in label
if(landscape)//depends on orientation
{
CGSize maximumSize = CGSizeMake(landscapeWidth, MAXFLOAT); // change width and height to your requirement
}
else //protrait
{
CGSize maximumSize = CGSizeMake(portraitWidth, MAXFLOAT); // change width and height to your requirement
}

//dynamic height of string depending on given width to fit
CGSize textSize = CGSizeZero;
if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")
{
NSMutableParagraphStyle *pstyle = [NSMutableParagraphStyle new];
pstyle.lineBreakMode = NSLineBreakByWordWrapping;

textSize = [[strText boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName :font,NSParagraphStyleAttributeName:[pstyle copy]} context:nil] size];
}
else // < (iOS 7.0)
{
textSize = [strText sizeWithFont:font constrainedToSize:maximumSize lineBreakMode:NSLineBreakByWordWrapping]
}

return (topPadding+textSize.height+bottomPadding) // caculate on your bases as u have string height
}
else
{
// return height from the storyboard
return [super tableView:tableView heightForRowAtIndexPath:indexPath];
}
}

EDIT : Added for support for > and < ios7 and as sizeWithFont method is deprecated in iOS 7.0

Automatically grow static table view cell height to fit label content height

From personal experience getting UITableCells to grow correctly is one of the most annoying things in iOS. The biggest gotcha is normally the constraints. If they are not perfect than the cell won't grow.

The key is to be able to draw a straight line from the top of the cell content view to the bottom of the content view. so from your example constants should look something like the following.

topView.top == contentView.top
label.top == topView.top
label.bottom == topView.bottom
topView.bottom == bottomView.top
bottomView.height == 40
bottomView.bottom == contentView.bottom

Without having that straight line the layout can't determine the correct height for the label to display all of the content.

So a rather simple version of what you are looking for in a new sample project:

class ViewController: UITableViewController {
@IBOutlet weak var label: UILabel!

override func viewDidLoad() {
super.viewDidLoad()

self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 80.0

label.text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
}

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
}

With the same layout:
Layout of Table Cell

Results in the following display in the simulator.
Result Image

Label in dynamic cell height on table view not showing text properly

To make the label expand with the cell, you should give it constraints that tie it to the views above and below -- in you case that looks like a constraint to the bottom of the cell, and one to the label (with "Titel...") above it.

Label height in Cell won't change height dynamically

Initially the tableViewCell take the height according to the label's text i.e. Loading...

Once you get Questions response from the Firebase, to reflect the changes in the tableViewCell's content and its height, you need to reload the whole tableView, i.e.

tableView.reloadData()

Call the above code once you receive the API response and after updating the dataSource that you're using for the tableView.

How do I dynamically resize a static UITableViewCell?

If you need to define the height of the cell at runtime, you must implement the tableView:heightForRowAtIndexPath: method of the UITableViewDelegate. It's a bit of a pain, but it's the only way you'll be able to programatically calculate the height of the cell and return it to the table when the table need to display it.

Static UITableView, make a single cell with dynamic height in Swift

Have you tried this?

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if (indexPath.row < 11) {
// Everything starts at 0, so this covers 0-10
// Whatever your cell height is
return <#fixedCellHeight#>
} else {
// This is your 12th cell (indexPath.row 11), as we start counting at 0
return UITableViewAutomaticDimension
}
}

Custom Tableview cell with multiline UILabel needs dynamic height

give constraint of header label top, leading, trailing to super view and bottom (vertical ) to description Label.
same for description label > leading, trailing and bottom margin to super view.

now set Header label height Fix and description label make multiline ( Lines = 0)

for table view set in viewDidLoad

_tableView.estimatedRowHeight = 100.0;
_tableView.rowHeight = UITableViewAutomaticDimension;

Let me know if this works...



Related Topics



Leave a reply



Submit