2017-05-14 93 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 
    } 
相关问题