Swift, Auto Resize Custom Table View Cells
Check if your constraints are like this(based on your image) :
imageView : set to Top and Leading of your container, with fix height and width values.
label : you can set it to top and horizontal space of your image, with fix height and width as well.
textView : leading to image, top space to label, trailing and bottom to container.
And keep using
tableView.estimatedRowHeight = 100
tableView.rowHeight = UITableViewAutomaticDimension
in your viewWillAppear()
Swift: Continuous resizing of tableViewCell while user types in a UITextView
- The constraints that determine the height should be laid out in such a way that the
textView
is attached directly to the top and bottom of thecontentView
or to views which are connected to the top and bottom of thecontentView
, so that autolayout can make out the height by connecting the constraints. - Make sure that you do not mention a height for the
textView
and disable scrolling. Let automatic dimension take care of all that. - Now all you need to do is call
tableView.beginUpdates()
andtableView.endUpdates()
ontextViewDidChange
Here is my repo which demonstrates the same.
OP Edit:
You should store the additional height that you add in a variable in the cell class so the cells can reload an appropriate height when the tableVIew is reloaded.
You should also change textViewDidChange method
cell.frame = CGRect(x: cell.frame.origin.x, y: cell.frame.origin.y, width: cell.frame.width, height: textView.frame.height)
to
let newFrame = ”originalCellHeight” - ”originalTextViewHeight” + textView.contentSize.height
cell.frame = CGRect(x: cell.frame.origin.x, y: cell.frame.origin.y, width: cell.frame.width, height: newFrame )`
Custom table cell not resizing to fit content view
If you are not using the storyboard and AutoLayout constraints then you need to manually calculate the height which the UILabels
are going to take based on the content it holds. I don't see any code which calculates the height based on content and accordingly setting the height of your labels and also the height of your custom cell.
You must implement heightForRowAtIndexPath
to set the height for your cells.
Auto-resizing table view cells with button added as subview
Summary : Here is an hand-made example about this problem.
First, read the answer about autoresizing tableviewcell with UILabel because you will do like that with a little changes.
The difference between UILabel and UIButton is UILabel has Intrinsic Content Size based on Width and Height. So you need to subclass UIButton and override method :
-(CGSize)intrinsicContentSize {
return CGSizeMake(self.frame.size.width, self.titleLabel.frame.size.height);
}
In your CustomCell class, override method
- (void)layoutSubviews{
[super layoutSubviews]; //btnTest is your Button
[self.contentView setNeedsLayout];
self.btnTest.titleLabel.preferredMaxLayoutWidth = CGRectGetWidth(self.btnTest.titleLabel.frame);
}
In your ViewController :
- (void)configureCell:(UITableViewCell *)cell forRowAtIndexPath: (NSIndexPath *)indexPath{
if ([cell isKindOfClass:[TestCell class]]) {
TestCell *testCell = (TestCell *)cell;
[testCell.btnTest setTitle:@"An example autoresizing UITableViewCell height based on UIButton added as subview by Matie.An example autoresizing UITableViewCell height based on UIButton added as subview by Matie.An example autoresizing UITableViewCell height based on UIButton added as subview by Matie." forState:UIControlStateNormal];
#warning You need to have this below line, to calculate height when the first time viewDidLoad.
testCell.btnTest.titleLabel.text = @"An example autoresizing UITableViewCell height based on UIButton added as subview by Matie.An example autoresizing UITableViewCell height based on UIButton added as subview by Matie.An example autoresizing UITableViewCell height based on UIButton added as subview by Matie.";
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
[self configureCell:self.prototypeCell forRowAtIndexPath:indexPath];
//
self.prototypeCell.bounds = CGRectMake(0.0f, 0.0f, CGRectGetWidth(self.tableView.bounds), CGRectGetHeight(self.prototypeCell.bounds));
[self.prototypeCell setNeedsLayout];
[self.prototypeCell layoutIfNeeded];
CGSize size = [self.prototypeCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
return size.height +1;
}
How do I tell my UITableViewCell to auto-resize when its content changes?
There is a documented way to do this. See UITableView.beginUpdates()
documentation:
You can also use this method followed by the endUpdates method to animate the change in the row heights without reloading the cell.
So, the correct solution is:
tableView.beginUpdates()
tableView.endUpdates()
Also note that there is a feature that is not documented - you can add a completion handler for the update animation here, too:
tableView.beginUpdates()
CATransaction.setCompletionBlock {
// this will be called when the update animation ends
}
tableView.endUpdates()
However, tread lightly, it's not documented (but it works because UITableView
uses a CATransaction
for the animation).
Related Topics
Swift Update Label (With HTML Content) Takes 1Min
Difference Between Associated and Raw Values in Swift Enumerations
How to Detect Absent Network Connection When Setting Firestore Document
How to Return a First Word from a String in Swift
How to Upload Zip Data with Alamofire
Swift - Firebase Search in Database
Swift Uialertcontroller with Pickerview Button Action Stay Up
Swiftui - Two Buttons in a List
Uicollectionview Compositionallayout Not Calling Uiscrolldelegate
Back Button Image - What Is It Called in Swift
How to Print a String from Plist Without "Optional"
Getting String Name of Objective-C @Objc Enum Value in Swift
Why I Can Change/Reassigned a Constant Value That Instantiated from a Class