![](/img/trans.png)
[英]I want a UITableView inside of a UIScrollView to extend to the bottom of the UIScrollView and not get cut off by initial screen
[英]Swift 3 - UITableView is cut off in UIScrollView
我的tableview有一個問題,在scrollview中。 桌子最后被切斷了。
這是我的故事板(表格禁用滾動)。 該按鈕只是我真實應用中某些內容的占位符:
這是表類(我知道它從昨天的問題我只是無法解決問題):
class Table: UITableView, UITableViewDataSource, UITableViewDelegate{
let sections: [String] = ["Section 1", "Section 2", "Section 3"]
let s1Data: [String] = ["Row 1", "Row 2", "Row 3"]
let s2Data: [String] = ["Row 1", "Row 2", "Row 3"]
let s3Data: [String] = ["Row 1", "Row 2", "Row 3"]
var sectionData: [Int: [String]] = [:]
override func awakeFromNib() {
super.awakeFromNib()
delegate = self
dataSource = self
register(UINib(nibName: "Cell", bundle: nil), forCellReuseIdentifier: "Cell")
sectionData = [0:s1Data, 1:s2Data, 2:s3Data]
rowHeight = UITableViewAutomaticDimension
estimatedRowHeight = 96.0
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int)
-> Int {
return (sectionData[section]?.count)!
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int)
-> String? {
return sections[section]
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return sections.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "Cell")
return cell!
}
}
單元格本身的高度為340,表中應為其中的三個。
ViewController對表的高度和表本身有約束。 我在stackoverflow上閱讀了有關此解決方案的內容。 當我在約束 - 常量 - 設置斷點時,高度約為1000,所以這一點是正確的:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var tableHeight: NSLayoutConstraint!
@IBOutlet weak var table: Table!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
tableHeight.constant = table.contentSize.height
}
}
我嘗試了不同的建議如何擺脫問題。 但不知怎的,我想念一些東西。 有人可能會給我一個建議。
在我看來,你沒有設置從tableview底部到它的superview底部的約束(scrollview中的View)。 如果是這樣,那么View將不會調整大小以適應其中的所有表
我根據您的要求設置了所有約束,我已經安裝了ScrollView和ContentView的頂部,左側,右側和底部約束,並將contentView的寬度和高度分配給主視圖,因此scrollview將自動變得更大,因為它的內容大小。
如果要滾動只是滾動不是你的tableView那么在這種情況下,你可以給TableView中的一個固定的高度約束,創建一個固定高度Constarint的NSLayoutConstraint的@IBOutlet並通過計算各行高度更新其不變!
第一種方法:
第二種方法:
在viewDidLoad方法中嘗試這個:
從底部為tableView提供填充:
在這里我給了tableView底部一個20.0的固定填充,你可以根據你的要求給出。
yourTableView.contentInset = UIEdgeInsets(top: 0.0, left: 0.0, bottom: 20.0, right: 0.0)
這是演示鏈接: https : //drive.google.com/file/d/0B-VsxWuYK8apODhXSnc1aVM5djQ/view
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.