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:
Dynamically adjust the height of the tableview cell based on content - iOS Swift
Use custom cell and labels.
Set up the constrains for the UILabel. (top, left, bottom, right)
Set lines of the UILabel to 0
Add the following code in the viewDidLoad method of the ViewController:
tableView.estimatedRowHeight = 68.0
tableView.rowHeight = UITableView.automaticDimension
// Delegate & data source
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableView.automaticDimension;
}
Swift 4:
// Delegate & data source
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
Swift 4.2:
// Delegate & data source
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
Cannot Set Dynamic TableView Row Height
If you want to set the dynamic tableview row height, you must use autolayout.
Please set the constraint on the UI components in the cell as shown in the image below.
I used UILabel as sample code. For UILabel, be sure to set Lines to 0.
Here is the sample code
class ViewController: UIViewController {
let stringArr = ["Swift Dynamic Table Cell HeightSwift Dynamic Table Cell Heightaa", "Swift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell Heightaaaa", "Swift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell Height", "Swift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell Heightaaaaaa", "Swift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell HeightSwift Dynamic Table Cell Heightaaaaaa", "bbbb"]
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.estimatedRowHeight = 100
tableView.rowHeight = UITableView.automaticDimension
}
}
extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return stringArr.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "TestCell", for: indexPath) as? TestCell else {
return UITableViewCell()
}
cell.label.text = stringArr[indexPath.row]
return cell
}
}
- You need to register the cell in the tableview.
tableView.register(UINib(nibName: “YourCellNibName”, bundle: nil), forCellReuseIdentifier: “YourCellReuseIdentifier”)
Dynamically change cell height according to label text
The bottom anchor needs to be set:
cevapLabel.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
Related Topics
Submitting iOS App to App Store Application Identifier Invalid
How to Set the Rootviewcontroller with Swift, iOS 7
JSON Parsing Using Nsjsonserialization in iOS
Static VS Class as Class Variable/Method (Swift)
Apple Push Notification Service Server Certificate Update
Make Swift Assume Degrees for Trigonometry Calculations
Play a Video from Youtube in a Avplayerviewcontroller in Swift
iOS Nsnotificationcenter to Check Whether the App Came from Background to Foreground
Date Formats from Device Based on Locale
How to Run Multiple iOS Simulators at Once
How to Prevent a Remote Notification from Being Displayed
Phonegap on iOS with Absolute Path Urls for Assets
Swift How to Use Nstimer Background
Dyld: Library Not Loaded for a Framework Within a Framework
The 'Pods' Target Has Transitive Dependencies That Include Static Binaries' When Installing Gcm
How to Create the Current Date (Or Any Date) as an Nsdate Without Hours, Minutes and Seconds