问题
image 1
image 2
As you see, i have storyboard like in pic 1, but in simulator it looks like in pic 2. How can i make it, thats this tableView inside View will be fullsized if contains some data. If not - it will be hidden.
At this moment, if its contains some data, UIview doesnt change the height and uitableview is almost not visible.
This lines didnt help:
tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableView.automaticDimension
------------------part 2--------here is code of viewController---------
import UIKit
class DetailsViewController: UIViewController {
@IBOutlet weak var image: UIImageView!
@IBOutlet weak var name: UILabel!
@IBOutlet weak var price: UILabel!
@IBOutlet weak var details: UILabel!
@IBOutlet weak var buttonAdd: UIButton!
@IBOutlet weak var variation: AutoSizingTableView!
var selectedImage: UIImage?
var selectedName: String?
var selectedPrice: String?
var selectedDetails: String?
var selectedVariation: NSDictionary?
override func viewDidLoad() {
super.viewDidLoad()
variation.delegate = self
variation.dataSource = self
variation.invalidateIntrinsicContentSize()
view.frame.size = variation.intrinsicContentSize
// view.frame.size = variation.intrinsicContentSize
variation.reloadData()
// variation.layoutIfNeeded()
// variation.frame.size = variation.intrinsicContentSize
// variation.translatesAutoresizingMaskIntoConstraints = false
variation.rowHeight = UITableView.automaticDimension
variation.isScrollEnabled = false
variation.isHidden = false
buttonAdd.layer.cornerRadius = 5
view.layer.cornerRadius = 20
view.clipsToBounds = true
image.layer.cornerRadius = 20
image.image = selectedImage
name.text = selectedName
price.text = selectedPrice
details.text = selectedDetails
// Do any additional setup after loading the view.
}
@IBAction func buttonTapped(_ sender: Any) {
print(selectedVariation?.allKeys.count)
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/
}
extension DetailsViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return selectedVariation?.allKeys.count ?? 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Variation", for: indexPath)
// if selectedVariation != nil {
// for i in selectedVariation {
cell.textLabel?.text = "test test"
return cell
}
}
class AutoSizingTableView: UITableView {
override var intrinsicContentSize: CGSize {
self.layoutIfNeeded()
return CGSize(width: UIView.noIntrinsicMetric, height: contentSize.height)
}
override var contentSize: CGSize {
didSet{
self.invalidateIntrinsicContentSize()
}
}
override func reloadData() {
super.reloadData()
self.invalidateIntrinsicContentSize()
}
}
回答1:
The function of estimatedRowHeight
and setting a rowHeight
for a tableView is to set the height for the tableView cells and not the height of the tableView itself.
As you insert or delete cells into your tableView, contentSize
of the tableView changes.
Solution 1:
You could use the below subclass of UITableView which automatically changes it's intrinsicContentSize
as it's contentSize
changes so AutoLayout can take care of the rest. Use this subclass instead of UITableView in your view controller.
If your tableView has no cells, the intrinsicContentSize
height could be set to 0.
I'd recommend you to read more about a view's intrinsic content size here.
class AutoSizingTableView: UITableView{
override var intrinsicContentSize: CGSize {
self.layoutIfNeeded()
return CGSize(width: UIView.noIntrinsicMetric, height: contentSize.height)
}
override var contentSize: CGSize {
didSet{
self.invalidateIntrinsicContentSize()
}
}
override func reloadData() {
super.reloadData()
self.invalidateIntrinsicContentSize()
}
}
Solution 2: You could manually add a height constraint to your tableView and change it's constant as required based on the contentSize or if you have additional requirements as per your design specifications.
来源:https://stackoverflow.com/questions/62730413/uiview-and-tableview-resizable