why UITableViewAutomaticDimension not working?
In order to make UITableViewAutomaticDimension work you have to set all left, right, bottom, and top constraints relative to cell container view. In your case you will need to add the missing bottom space to superview constraint for label under the title
UITableViewAutomaticDimension not working with custom table cell
Automatic height works on calculated cell's contentView
height. You therefore have to add constraints that will be used to calculate its real height.
Try this:
class UINoteTabelViewCell: UITableViewCell {
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.selectionStyle = .none
}
func fill(_ note: Note) {
let view = UINote(withNote: note, atPoint: .zero)
self.contentView.addSubview(view)
// this will make the difference to calculate it based on view's size:
view.translatesAutoresizingMaskIntoConstraints = false
view.leftAnchor.constraint(equalTo: contentView.leftAnchor).isActive = true
view.rightAnchor.constraint(equalTo: contentView.rightAnchor).isActive = true
view.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
view.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
}
}
Also, estimatedRowHeight
should be set to a specific value that approximately estimates the size of all cells. But that would not be a problem.
UITableViewAutomaticDimension works not as expected. Swift
You're doing a number of things wrong, but the main point is your use of greaterThanOrEqualTo:
.
Instead, it should be:
cellCaption.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -8),
Also, your current code is adding a new label as a subview every time you set the text. Cells are reused, so you only want to add the label when the cell is created.
Next, the correct properties for the table are:
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 44
Put those two lines in viewDidLoad()
of your table view controller, and do not implement heightForRowAt
or estimatedHeightForRowAt
functions. You can delete your extension
entirely.
And finally, you only need to set the constraints once. Definitely NOT in layoutSubviews()
.
Here's a full example:
//
// NotesTableViewController.swift
//
// Created by Don Mag on 8/29/18.
//
import UIKit
class NotesModel: NSObject {
var name: String = ""
}
class NotesCell: UITableViewCell {
lazy private var cellCaption: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.font = UIFont.systemFont(ofSize: 20, weight: UIFont.Weight.medium)
label.numberOfLines = 0
label.lineBreakMode = .byWordWrapping
return label
}()
func configure(with note: NotesModel) {
cellCaption.text = note.name
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
func commonInit() -> Void {
contentView.addSubview(cellCaption)
NSLayoutConstraint.activate([
cellCaption.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 8),
cellCaption.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 8),
cellCaption.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -8),
cellCaption.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -8),
])
}
}
class NotesTableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 44
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 8
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "NotesCell", for: indexPath) as! NotesCell
let m = NotesModel()
if indexPath.row == 3 {
m.name = "This is a very long caption. It will demonstrate how the cell height is auto-sized when the text is long enough to wrap to multiple lines."
} else {
m.name = "Caption \(indexPath.row)"
}
cell.configure(with: m)
return cell
}
}
Result:
UITableViewAutomaticDimension not working on iOS 8
add following code in "cellForRowAtIndexPath" method before return cell.
cell.setNeedsUpdateConstraints()
cell.updateConstraintsIfNeeded()
Swift UITableViewAutomaticDimension is not working
You needs to set the constraints on your cell objects like this so the cell can calculate the size needed.
Related Topics
Swift Can Not Save .M3U8 File to Gallery
Convert Integer to Roman Numeral String in Swift
Diddiscoverservices Is Not Being Called After a Ble Connection
Should I Remove All Skspritenodes and Labels When I Switch from One Node to Another
How to Decode Utf8-Literals Like "\Xc3\Xa6" in Swift 5
Get Images from Document Directory Not File Path Swift 3
Initializer for Conditional Binding Must Have Optional Type, Not '[String:Any]'
Can the Byte Order of Double Be Safely Reversed
How Is a Global Variable Set to Private Understood in Swift
Does _Arraytype or _Arrayprotocol Not Available in Swift 3.1
How to Sort an Array of Posts by Their Elements
Where to Place App Delegate Code in App.Swift File
Presenting a Uiviewcontroller from Skscene Shows Black Screen
Setting Observer for Swift Objects/Properties
How to Deal with Commas When Writing Objects to CSV in Swift
Resizing Large Resolution Images Producing 1000X1000 Pixels Size When Size Is Set to 500X500 Pixels
Swift Check If Value Is of Type Array (Of Any Type)
How to Make a Uiview Grow in Uiview.Animate() with an Accurate Corner Radius