2017-07-13 121 views
1

我能夠根據tableview內容高度給出scrollview的高度,但有時scrollview高度不滾動,有時scrollview高度給了我基於tableview的完美高度,我很困惑,它不會給我每一次,每當我運行的應用程序它沒有給完美的高度基於tableview。在我的兩個tableview數據都來自網絡,它是我沒有得到基於tableview高度的scrollview高度,因爲互聯網慢所以它加載數據緩慢tableview?如何根據UITableView高度動態設置UIScrollView高度?

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 
     } 

    } 

或者我在代碼中遺漏了一些我沒有收到的東西,可以解決這個問題嗎?

+0

內容的大小,爲什麼你想改變滾動視圖的高度,而不是它的contentsize? –

回答

1

工作方式如下goods.just檢查出來

讓IBOutlets了滾動,內容查看,爲的tableView的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) 
} 
+0

@IBOutlet var heightConstraints:NSLayoutConstraints!這是爲viewHeight? –

+0

添加此代碼,它不會向下滾動 –

+0

@BikeshThakur heightConstraints for tableViewheight –