首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何根据UITableView height动态设置UIScrollView height?

如何根据UITableView height动态设置UIScrollView height?
EN

Stack Overflow用户
提问于 2017-07-13 14:08:24
回答 1查看 1.8K关注 0票数 1

我可以给出滚动视图的高度基于表格视图的内容高度,但有时滚动视图高度不滚动,有时滚动视图高度是给我完美的高度基于表格视图,我感到困惑,它没有给我每次和每次我运行的应用程序它没有给出基于表格视图的完美高度。在我的两个tableview数据都来自Web,我没有得到基于tableview高度的滚动视图高度,因为互联网速度很慢,所以在tableview中加载数据很慢??

代码语言:javascript
复制
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
        }

    }

或者我在代码中遗漏了一些我没有得到的东西,这个问题能解决吗?

EN

回答 1

Stack Overflow用户

发布于 2017-07-13 15:51:21

下面是goods.just的工作方式

为scrollView、ContentView、tableView设置IBOutlets,为tableView设置高度约束(根据内容调整高度)

代码语言:javascript
复制
@IBOutlet weak var scrollView:UIScrollView!
@IBOutlet weak var contentView:UIView!
@IBOutlet weak var tableView:UITableView!

@IBOutlet var heightConstraints:NSLayoutConstraints!

viewDidLoad方法中

代码语言:javascript
复制
    // 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方法中

代码语言:javascript
复制
// calling to update scrollView contentSize after all views initialize done.
self.updateScrollViewContentSize()

一种计算UIScrollView内容大小的方法

代码语言:javascript
复制
@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)
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45072621

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档