I have a UITableView in which i am displaying a custom cell.I my cell i have two label & one view as below in picture.
I have given constraint of left v
Give your label constraint as top to Cell , Bottom to cell and keep your Leading and trailing constraint as it is. In
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewAutomaticDimension;
}
-(CGFloat)tableView:(UITableView *)tableView
estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewAutomaticDimension;
}
by using UITableViewAutomaticDimension will grow your tableview cell height according to your label's content and most important point is , select your label then go to Identity Inspector and in number of lines , write 0
To set automatic dimension for row height & estimated row height, ensure following steps to make, auto dimension effective for cell/row height layout.
UITableViewAutomaticDimension
to rowHeight & estimatedRowHeightheightForRowAt
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
// Swift 4.2 onwards
table.rowHeight = UITableView.automaticDimension
table.estimatedRowHeight = UITableView.automaticDimension
// Swift 4.1 and below
table.rowHeight = UITableViewAutomaticDimension
table.estimatedRowHeight = UITableViewAutomaticDimension
}
// UITableViewAutomaticDimension calculates height of label contents/text
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
// Swift 4.2 onwards
return UITableView.automaticDimension
// Swift 4.1 and below
return UITableViewAutomaticDimension
}
For label instance in UITableviewCell
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.
This is how I worked out.
1) Set no. of lines to 0.
2) Set LineBreakage.
3) Add Constraints.
4) Change vertical content hugging priority.
5) On ViewDidLoad:
override func viewDidLoad() {
super.viewDidLoad()
self.sampleTableView.estimatedRowHeight = 600.0;
self.sampleTableView.rowHeight = UITableViewAutomaticDimension;
self.sampleTableView.setNeedsLayout()
self.sampleTableView.layoutIfNeeded()
}
6) On TableView Custom Cell:
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
sizeToFit()
layoutIfNeeded()
}
First of all you should not calculate height manually in auto layout environment. Just set both labels TopSpace and BottomSpace to cell's contentView and make sure you set both labels NumberOfLines to 0 and LineBreakMode to WordWrap.
And the other constraint are as below,
ItemLabel:
SeparatorView:
DescriptionLabel:
And add the delegates for height as below,
#pragma mark - UITableView Delegates
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 44.0;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewAutomaticDimension;
}
You should get the output as below,
Hope this would help you.
Swift 3 / Swift 4
Custom cell:
class CustomCell: UITableViewCell {
@IBOutlet var messageLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
sizeToFit()
layoutIfNeeded()
}
}
viewDidLoad:
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.estimatedRowHeight = 80
self.tableView.rowHeight = UITableViewAutomaticDimension
}
And cellForRowAtIndexPath:
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell : CustomCell = tableView.dequeueReusableCell(withIdentifier: "yourcellIdentifier", for: indexPath) as! CustomCell
cell.messageLabel.text = exampleContent[indexPath.row]
cell.messageLabel.sizeToFit()
cell.messageLabel.layoutIfNeeded()
return cell
}
You need to define maximum estimated Height when view is loading
tblObject.estimatedRowHeight = 300;
tblObject.rowHeight = UITableViewAutomaticDimension;