2017-05-14 94 views
0

我想使UITableview與充滿UITableview剩餘的空白空間的頁腳視圖。然而,問題在於細胞具有不同的高度。UITableview與動態高度單元格和填充頁腳視圖

我知道,如果單元格都是靜態高度,我可以簡單地計算出有多少單元格,並將其乘以靜態高度。不幸的是,這在這種情況下不起作用。

當前解決方案:

適用於靜態高度單元。

對於動態高度單元格不起作用。

private func adjustFooterHeight() { 
     if data.count == 0 { 
      footerView.frame.size.height = tableView.bounds.height 
     } 
     else { 
      //I use data.count instead of (data.count - 1) as I have the extra hidden cell, as pictured in image 2. 
      let bottomFrame = tableView.rectForRow(at: IndexPath(row: data.count, section: 0)) 
      let height = tableView.bounds.height - (bottomFrame.origin.y + bottomFrame.height - rowHeight) 
      footerView.frame.size.height = height < 0 ? 0 : height 
      tableView.reloadData() 
     } 
    } 

意向的圖:

enter image description here

+0

想要用非常自定義的視圖來填充它還是隻想讓TableView的其餘部分變成純色?白色例如? –

+0

我並不是特別希望視圖有很多內容,但它需要使表視圖的內容達到視圖控制器框架的底部。一旦表視圖有足夠的單元格超過屏幕的大小,這很好,我不需要它,但在那之前,我會這樣做。我使用這個頁腳視圖來使表格視圖可滾動,因爲我的表格視圖的第一個單元隱藏在視圖控制器上方。這有點像隱藏的下拉操作。我會用一兩張圖片更新我的帖子。 – Connor

+0

上下文的圖片會很棒:) –

回答

0

所以我想通了......

這裏是解決方案。

private func adjustFooterHeight() { 

     //Get content height & calculate new footer height. 
     let cells = self.tableView.visibleCells 
     var height: CGFloat = 0 
     for i in 0..<cells.count { 
      height += cells[i].frame.height 
     } 
     height = self.tableView.bounds.height - ceil(height) 

     //If the footer's new height is negative, we make it 0, since we don't need footer anymore. 
     height = height > 0 ? height : 0 

     //Create the footer 
     let footerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: height)) 
     self.tableView.tableFooterView = footerView 
    } 
相關問題