Dynamically increase height of UILabel & TableView Cell?
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.
Changing UILabel text length in UITableViewCell with dynamic height does not resize it correctly
I have found a solution that works, after updating the UILabel content I am calling:
UIView.animateWithDuration(0.3) {
cell.contentView.layoutIfNeeded()
}
To update cell constraints. And then to resize the cell height:
tableView.beginUpdates()
tableView.endUpdates()
I will wait to accept this answer to see if someone else will answer a more elegant solution though.
Dynamically change cell height according to label text
The bottom anchor needs to be set:
cevapLabel.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
Multiple UILabels in UITAbleViewCell with dynamic height
Add a (lower priority) fixed height constraint for the cell (e.g. = 40).
Add a top and bottom constraint for each label. Make the top constraint a fixed margin (e.g. = 8). Make the bottom constraint a (higher priority) greater than or equal constraint (e.g. >= 8).
The cell's height will be determined by the taller label, and all the other vertical constraints will still be met.
Update:
It turns out that you don't need a fixed height constraint for the cell, or even need to tweak the horizontal content hugging or compression resistance priorities.
I pinned the labels' top and left or right edge to the superview.margin, = 0, and added a horizontal space between the labels >= 4.
I pinned the labels' bottom edge to the superview.margin, >= 0. I also set both labels' vertical compression resistance priority to 1000.
At this point, we've set the minimum necessary constraints. However, if both labels are long, one label will take most of the width, forcing the other label to be extremely narrow. I arbitrarily added a width >= 100 constraint to both labels to balance things out.
Enable self-sizing in -viewDidLoad
, then assign your labels' text, and have the cell lay itself out. It wasn't necessary to override anything in the custom UITableViewCell
.
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.estimatedRowHeight = 44.0;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
cell.leftLabel.text = ...;
cell.rightLabel.text = ...;
[cell setNeedsLayout];
[cell layoutIfNeeded];
return cell;
}
Related Topics
Singleton in iOS Objective C Doesn't Prevent More Than One Instance
Uitableview Auto Resizing Row Constraint Breaking Mysteriously on iPhone 6Plus
What Is Most Common and Correct Practice to Get a Cgfloat from an Nsnumber
Uislider That Snaps to a Fixed Number of Steps (Like Text Size in the iOS 7 Settings App)
Presenting Viewcontroller with Navigationviewcontroller Swift
How to Connect Viewcontroller.Swift to Viewcontroller in Storyboard
How to Remove Tmp Directory Files of an iOS App
Uitableview Reloaddata Automatically Calls Resignfirstresponder
Multiple Locations on Map (Using Mkmapitem and Clgeocoder)
Get the Frame of Uibarbuttonitem in Swift
Weird Behaviour of Xcode 11 Debugger - Showing Values as Nil When There's a Value
Are Headphones Plugged In? iOS7
Linking Child View Controllers to a Parent View Controller Within Storyboard
Xcode 8 - Missing Files Warnings