我的问题是我正在使用自动调整大小的单元格以及估计的行高。
我的tableview是scrollview中的不可滚动表。我有一个桌子高度的变量,最初将其设置为0。
我正在尝试将我的桌子高度更新为合适的大小,具体取决于要填充多少单元格以及这些单元格占用的大小。我正在尝试使用tableview.contentSize.height
来获取表格的高度并将变量设置为该大小。
然后,我想起我的对齐功能以重置桌子高度。当我构建和运行表时,表的大小不足以显示它应该显示的所有单元格。
我怎样才能解决这个问题?
最佳答案
您可以通过以下步骤->
3.在表视图中加载数据并获取表视图的ContentHeight
更新约束后不要忘记调用layoutIfNeeded()
class ViewController: UIViewController {
@IBOutlet weak var containerViewHeightConstraint: NSLayoutConstraint!
@IBOutlet weak var nameTableView: UITableView!
var dataArrary = [String]()
override func viewDidLoad() {
super.viewDidLoad()
nameTableView.rowHeight = UITableViewAutomaticDimension
nameTableView.estimatedRowHeight = 44.0
nameTableView.tableFooterView = UIView(frame:CGRectZero)
nameTableView.tableHeaderView = UIView(frame:CGRectZero)
dataArrary = ["item1 bshfklsdflkjsdfkljsdklfjlsdkfjlksdjflksdjflkdsjflkdsjflksdjflkjdslkfjdslkjflkdsfjdsfkljsdflkjdslkfj", "item2hfdshjgfhjdsgfhjdgfhjdgfhjdgfhjgfhsdf", "item3", "item4", "item5", "item6kfjdskfljsdlkfjlksdfjlksdjfkldsjfklfjdkslf"]
}
override func viewDidAppear(animated: Bool) {
updateView()
}
func updateView() {
self.nameTableView.scrollEnabled = false
self.nameTableView.frame.size = CGSizeMake(self.view.frame.width, self.nameTableView.contentSize.height)
containerViewHeightConstraint.constant = self.nameTableView.frame.height
// assign height equal to the tableView's Frame
self.view.layoutIfNeeded()
// redraws views and subviews with updated constraint
}
}
extension ViewController:UITableViewDataSource {
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataArrary.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
cell.textLabel?.numberOfLines = 0
cell.textLabel?.text = dataArrary[indexPath.row]
return cell
}
}
关于ios - 使用自定义大小的单元格和不可滚动的表格时调整tableView的高度,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38230195/