我可以给出滚动视图的高度基于表格视图的内容高度,但有时滚动视图高度不滚动,有时滚动视图高度是给我完美的高度基于表格视图,我感到困惑,它没有给我每次和每次我运行的应用程序它没有给出基于表格视图的完美高度。在我的两个tableview数据都来自Web,我没有得到基于tableview高度的滚动视图高度,因为互联网速度很慢,所以在tableview中加载数据很慢??
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView == tableview{
let cell = tableview.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! noticeTableViewCell
let noticedata = notices[indexPath.row]
cell.noticeText.text = noticedata.notice
tableviewheightConstraint.constant = tableView.contentSize.height
// scrollview.contentSize = CGSize(width: self.view.frame.size.width, height: tableview.frame.origin.y + tableView.contentSize.height )
return cell
} else {
let cell = pendingtableview.dequeueReusableCell(withIdentifier: "pendingbillcell", for: indexPath) as! PendingbillTableViewCell
let getdata = pendingbillsdata[indexPath.row]
let date = getdata.date
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
let dateFromString : NSDate = dateFormatter.date(from: date)! as NSDate
dateFormatter.dateFormat = "dd-MM-yyyy"
let datenew = dateFormatter.string(from: dateFromString as Date)
cell.billnotext.text = getdata.Billno
cell.datetext.text = datenew
cell.amounttext.text = getdata.amount
cell.statustext.text = getdata.status
pendingtableviewheightConstraint.constant = tableView.contentSize.height
scrollview.contentSize = CGSize(width: self.view.frame.size.width, height: pendingtableview.frame.origin.y + tableView.contentSize.height)
return cell
}
}或者我在代码中遗漏了一些我没有得到的东西,这个问题能解决吗?
发布于 2017-07-13 15:51:21
下面是goods.just的工作方式
为scrollView、ContentView、tableView设置IBOutlets,为tableView设置高度约束(根据内容调整高度)
@IBOutlet weak var scrollView:UIScrollView!
@IBOutlet weak var contentView:UIView!
@IBOutlet weak var tableView:UITableView!
@IBOutlet var heightConstraints:NSLayoutConstraints!viewDidLoad方法中
// set delagte & datasource to tableView
self.tableView.dataSource = self
self.tableView.delagte = self
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 40
// force the layout of subviews before drawing
self.defineTableView.layoutIfNeeded()
// Observer for oreientation change
NotificationCenter.default.addObserver(self, selector: #selector(self.updateScrollViewContentSize), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)viewDidAppear方法中
// calling to update scrollView contentSize after all views initialize done.
self.updateScrollViewContentSize()一种计算UIScrollView内容大小的方法
@objc private func updateScrollViewContentSize(){
// set dynamic height constant of tableView
self.heightConstraints.constant = self.tableView.contentSize.height + 10.0
var heightOfSubViews:CGFloat = 0.0
for views in self.contentView.subviews {
if views is UITableView {
heightOfSubViews = heightOfSubViews + (views as! UITableView).contentSize.height + 10.0
}else {
heightOfSubViews = heightOfSubViews + views.frame.size.height
}
}
self.scrollView.contentSize = CGSize.init(width: self.scrollView.frame.size.width, height: heightOfSubViews + 50.0) // 50.0 - space b/w controls(30.0) + extras spaces (20.0)
}https://stackoverflow.com/questions/45072621
复制相似问题