【问题标题】:How to Set Dynamically UIScrollView height based on UITableView height?如何根据 UITableView 高度动态设置 UIScrollView 高度?
【发布时间】:2017-12-17 18:19:35
【问题描述】:

我可以根据 tableview 内容高度给出滚动视图的高度,但有时滚动视图高度不滚动,有时滚动视图高度给我基于 tableview 的完美高度,我很困惑它没有每次都给我我运行应用程序,它没有根据 tableview 给出完美的高度。在我的两个 tableview 数据都来自 Web 并且由于 Internet 速度慢,我没有根据 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
        }

    }

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

【问题讨论】:

  • 为什么你想改变滚动视图的高度而不是它的内容大小?

标签: ios uitableview swift3 uiscrollview


【解决方案1】:

下面的方法行得通,看看就行了

为scrollView、ContentView、tableView和tableView的高度约束制作IBOutlets(根据内容调整高度)

@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)
}

【讨论】:

  • @IBOutlet var heightConstraints:NSLayoutConstraints!这是 viewHeight 吗??
  • @BikeshThakur heightConstraints for tableViewheight
  • 它不更新滚动视图高度或视图高度
  • @BikeshThakur 我没有发布基于内容限制 tableView 高度的 scrollView 高度。
  • 为什么你想改变滚动视图的高度而不是它的内容大小?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-27
  • 2016-11-08
  • 2013-04-28
  • 1970-01-01
相关资源
最近更新 更多