【发布时间】:2020-07-04 14:35:07
【问题描述】:
如您所见,我有图 1 中的情节提要,但在模拟器中它看起来像图 2。 我怎样才能做到这一点,如果包含一些数据,那么 View 中的这个 tableView 将是完整的。如果没有 - 它将被隐藏。
此时,如果它包含一些数据,UIview不会改变高度,uitableview几乎不可见。
这行没有帮助:
tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableView.automaticDimension
------------------part 2--------这里是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()
}
}
【问题讨论】:
标签: ios swift uiview tableview