Change the UITableViewCell Height According to Amount of Text
Based on the code you have provided, I think you are increasing only the cell height and not the cell.textLabel's height.
Ideally, you should set the frame size of cell.textLabel and the cell for you to see the full text in the cell.
A neat way to see whats wrong with a view in terms of size, is to color it different than the background (try setting cell.textLabel background to yellow) and see if the height is actually being set.
Here's how it should be
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
cell.textLabel.numberOfLines = 0;
cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:17.0];
NSString *cellText = @"Go get some text for your cell.";
UIFont *cellFont = cell.textLabel.font;
CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
cell.textlabel.frame.size = labelSize;
cell.text = cellText;
}
Hope this helps!
update: This is quite an old answer, and many lines in this answer may be deprecated.
How to change tableview cell height according to label text swift?
Considering you have already applied constraints on the label. And also set your number of lines to 0. And then paste the following code in viewDidLoad method
tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableViewAutomaticDimension
Dynamically change cell height according to label text
The bottom anchor needs to be set:
cevapLabel.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
UITableViewCell auto height based on amount of UILabel text
To go a little further on Dao's answer..
He is correct, you need to use UITableViewAutomaticDimension
Also, you need to ensure that you setup your constraints in a way that all of the content in the cell is constrained to the cells contentView. So your label will likely need constraints such as
- Leading constraint to ImageView
- Top constraint to contentView
- Bottom constraint to contentView
- Trailing constraint to contentView
Make sure that you set the UILabel to multiline (or lines = 0) and it should work.
If you are using the heightForRowAt delegate functions ensure you return UITableViewAutomaticDimension
Adjust UITableViewCell height according to the resizing label
In your case you've to manually manage autolayout constraints when only button is visible and after clicking text will be visible.
I've prepared a demo for that which you can found here.
https://github.com/rushisangani/TableViewCellExpand
Please refer storyboard constraints for TableViewCell and manage that wisely.
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.
Related Topics
Shift Elements in Array by Index
Silent Push Notification in iOS 7 Does Not Work
How to Rotate Text for Uibutton and Uilabel in Objective-C
Nsurlconnection Sendasynchronousrequest Can't Get Variable Out of Closure
Scrollview Gesture Recognizer Eating All Touch Events
Cannot Change Search Bar Background Color
How Can a Web Application Send Push Notifications to iOS Devices
Customizing the More Menu on a Tab Bar
Is It Considered a Private API to Use App-Prefs:Root
Uicollectionview: Animate Cell Size Change on Selection
iOS 8 Sdk: Modal Uiwebview and Camera/Image Picker
How to Recognize Which Pin Was Tapped
React Native Responsive Font Size
Remove Gradient Background from Uiwebview
Counting the Number of Lines in a Uitextview, Lines Wrapped by Frame Size
Why Storyboard UI Elements Not Showing on Uiviewcontroller in Xcode 6