UITableViewCell Height Issue
Well, you can't achieve the dynamic height of cell with UITableViewAutomaticDimension on the basis of image's constraints.
BUT image's height and width can help you in this. There is a plenty of help regarding this issue is available in following answer:
Dynamic UIImageView Size Within UITableView
UITableView Custom Cell Height Not Working
You haven't implemented the UITableViewDelegate
In your setupTable()
add the line
tableView.delegate = self
Then change your extension from
extension ViewController: UITableViewDataSource {
to extension ViewController: UITableViewDataSource, UITableViewDelegate {
How to use dynamic height of UITableViewCell in auto-layout and move other views to up when bottom view is hidden?
At first, make the height of UITableViewCell to UITableView.automaticDimension
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
Embed all of your questionLabels in a UIStackView
(vertical) excluding bottomLabel
. Set AutoLayoutConstraint between UIStackView
and bottomLabel
.
Set the numberOfLines
property of UILabel
s to 0(zero).
Set the Distribution of the UIStackView
as Fill
Then, in your tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
method hide the labels. And it will automatically handle the spaces between UILabel
s
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath) as! MyCell
cell.questionLabel1.text = labelOneText[indexPath.row]
cell.questionLabel2.text = labelTwoText[indexPath.row]
cell.questionLabel3.text = labelThreeText[indexPath.row]
if labelOneText[indexPath.row] == "" {
cell.questionLabel1.isHidden = true
}
if labelTwoText[indexPath.row] == "" {
cell.questionLabel2.isHidden = true
}
if labelThreeText[indexPath.row] == "" {
cell.questionLabel3.isHidden = true
}
return cell
}
Final Output:
UITableViewCell height auto layout not working on iOS 10
This configuration from your viewDidLoad
:
tableView.estimatedRowHeight = UITableViewAutomaticDimension
tableView.rowHeight = UITableViewAutomaticDimension
...works only on iOS 11. It is an iOS 11 innovation and does not apply to earlier systems.
For iOS 10, you must supply an actual estimatedRowHeight
value (such as 60
) to turn on automatic variable row height calculation.
Related Topics
When Should I Use Anyobject Insted of Uibutton in Swift
Items in File Provider Extension for iOS 11
How to Store JSON Decodable Values into Coredata Using Swift
How to Add Interactive Uilabels on Top of a Uiimageview
How to Get Alexa Working on My iOS App
How to Call Presentviewcontroller in Uiview Class
Icloud Document Picker from Wkwebview Dismissing Container View
Prevent Uitableview Scrolling Below a Certain Point
How to Get Multiple Buttons from a Single Tableviewcell
Swiftui How Add Custom Modifier with Callback
Spritekit/Swift - How to Check Contact of Two Nodes When They Are Already in Contact
How to Enable Only Numeric Digit Keyboard iOS Swift
Avaudiocompressedbuffer to Uint8 Array and Vice Versa
How to Monitor More Than 20 Regions
How to Crop a 4*3 Image in Photolibrary or Camera in Swift
Cannot Convert Value of Type 'String.Type' to Expected Argument Type 'String!'