3

I want to resize custom cell height dynamically.

In my tableView there are three custom columns :

  1. Product Name
  2. Product Qty
  3. Product Price

Now Product Name column does not show full name. Its shows only whatever text fits. But not able to show full name.

Though I've set heightForRowAtIndexPath as follows :

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return UITableViewAutomaticDimension;
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewAutomaticDimension;
}

This is below lines are set in my CustomCell class

self.productNameLabel.lineBreakMode = NSLineBreakByWordWrapping;
self.productNameLabel.numberOfLines = 0; 

Example:

NSString *prodName = @"This is my product name which may contain description with it";

So in above example, my UITableViewCell shows only this much product name: This is my product and it escapes further string.

See below attached property section and constraints...

enter image description here

enter image description here

I want to set product name in Nutrient column

enter image description here

Please help me out, tried every possible solution but not able to resolve this.

This way my uitableview data is showing...but not able to show full name in nutrient.

enter image description here

Faysal Ahmed
  • 7,501
  • 5
  • 28
  • 50

4 Answers4

3

First of all, estimatedRowHeight should be set to some specific number. It's an estimation that the tableView uses to calculate scroll indicator size, but does not really use it to size cells. So ideally you shoudl set something like:

tableView.estimatedRowHeight = 44
tableView.rowHeight = UITableViewAutomaticDimension

Then make sure that your cells have defined autolayout constraints that can be used to define their size, specifically height. In your case, make sure that the label top anchor is constrained to the cell's contentView.topAnchor, and bottom anchor is constrained to the cell's contentView.bottomAnchor, and leading and trailing of label to leading and trailing of contentView. Thus, when the label has more of the content, it will try to expand, and so should the cell.

Consider using UIStackView to manage cell's content, because then even expanding and collapsing of the cells will easier to manage. If that's your goal, see my answer to another question.

Milan Nosáľ
  • 19,169
  • 4
  • 55
  • 90
  • Please check my edited question, I've tried but yet not done. –  Jan 19 '18 at 11:04
  • @Aadi the way you are showing those constraints we cannot read half of them.. but there is something that's definitely not ok - bottom of the nutrient label is anchored to something called `NutritionTa...`.. it should go to cell's bottom (label's superview, just like for the top of the label) – Milan Nosáľ Jan 19 '18 at 11:15
0

Set these values for your label in Size Inspector(as shown in image) & don't set the height constraint for your label.

Shivam Tripathi
  • 1,405
  • 3
  • 19
  • 37
0

You have done good but need some more. First you set. tableView.estimatedRowHeight = 44 After that bottom constraints which you are given equal to 13 just make it greater than or equal to 13. It will be works for you.

0

To set automatic dimension for row height & estimated row height, ensure following steps to make, auto dimension effective for cell/row height layout. I just tested following steps and code and works fine.

  • Assign and implement tableview dataSource and delegate
  • Assign UITableViewAutomaticDimension to rowHeight & estimatedRowHeight
  • Implement delegate/dataSource methods (i.e. heightForRowAt and return a value UITableViewAutomaticDimension to it)

-

Objective C:

// in ViewController.h
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>

  @property IBOutlet UITableView * table;

@end

// in ViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];
    self.table.dataSource = self;
    self.table.delegate = self;

    self.table.rowHeight = UITableViewAutomaticDimension;
    self.table.estimatedRowHeight = UITableViewAutomaticDimension;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return UITableViewAutomaticDimension;
}

Swift:

@IBOutlet weak var table: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()

    // Don't forget to set dataSource and delegate for table
    table.dataSource = self
    table.delegate = self

    // Set automatic dimensions for row height
    table.rowHeight = UITableViewAutomaticDimension
    table.estimatedRowHeight = UITableViewAutomaticDimension
}



// UITableViewAutomaticDimension calculates height of label contents/text
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

For label instance in UITableviewCell

  • Set number of lines = 0 (& line break mode = truncate tail)
  • Set all constraints (top, bottom, right left) with respect to its superview/ cell container.
  • Optional: Set minimum height for label, if you want minimum vertical area covered by label, even if there is no data.

enter image description here

Note: If you've more than one labels (UIElements) with dynamic length, which should be adjusted according to its content size: Adjust 'Content Hugging and Compression Resistance Priority` for labels which you want to expand/compress with higher priority.

Krunal
  • 77,632
  • 48
  • 245
  • 261