【问题标题】:How to make last row of uitableview fill the empty space如何使uitableview的最后一行填充空白
【发布时间】:2016-04-08 13:46:58
【问题描述】:

我希望最后一个单元格/行 (mapView) 填充下面的空白区域。

我的 tableviewcontroller 代码

class CustomTableViewController: UITableViewController {

    let fakeArray = ["Row1","Row2","Row3"] // Array can contains more items

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return fakeArray.count+1
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        if(indexPath.row != fakeArray.count){
            let cellIdentifier = "TestTableViewCell"
            let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! TestTableViewCell
            cell.rowName.text = fakeArray[indexPath.row]
            return cell
        }else{
            let cellIdentifier = "MapTableViewCell"
            let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! MapTableViewCell
            return cell
        }

    }

    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        if(indexPath.row != fakeArray.count){
            return 44
        }else{
            return 80
        }
    }

}

有时我会在地图之前显示更多行。所以下面的空白区域可能会有所不同。当然,空白空间也会因 iPhone 屏幕尺寸而异。

【问题讨论】:

  • 您可以增加最后一行的行高。
  • 我会将地图放在表格视图的页脚视图中。
  • 您会在地图前显示这么多行以致地图不可见吗?然后呢?
  • @MikeTaverne 是的,我会的。理想情况下,我想要这个案例的标准高度。当我有空的空间时,我想调整单元格的高度来填充这个空间

标签: ios swift uitableview


【解决方案1】:

您需要弄清楚您有多少空白空间,然后将地图单元格的高度设置为该值。

首先,将 table view 的顶部和底部约束到 superview,以确保它占据整个屏幕。

您的表格视图的可见高度将是:

    self.tableView.bounds.size.height

在 heightForRowAtIndexPath 中,计算地图单元格上方的单元格高度,并返回与表格视图可见高度之间的差值:

    let otherCellsHeight: CGFloat = fakeArray.count * 44

    return self.tableView.bounds.size.height - otherCellsHeight

当然,为了安全起见,您还应该检查地图单元格的计算高度是否为负数或小于某个最小高度:

    let availableSpace = self.tableView.bounds.size.height - otherCellsHeight

    return (availableSpace > 80) ? availableSpace : 80

【讨论】:

    【解决方案2】:

    此答案适用于可能有太多不同高度的不同单元格的情况,因此计算单元格 0..n-1 的高度并不像@Mike Taverne 的答案那么容易。

    在我的情况下,最后一个单元格应该垂直扩展,但应该有一个最小高度,所以它看起来不错。在这种情况下,tableView 是可滚动的。

    代码如下:

    public function tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if item(for: indexPath) == "some condition" {
            return xxx // some number
        else if item(for: indexPath) == "some other condition" {
            return yyy // some other height
        } ...
        else { // I KNOW THIS CELL WILL BE LAST 
            let minimalLastCellHeight = 200
            guard let previousCell = tableView.visibleCells.last else {
                return minimalLastCellHeight
            }
            let lowestYCoordinate = previousCell.frame.maxY // low corner Y coordinate of previous cell
            return max(availableHeight - lowestYCoordinate, minimalLastCellHeight)
    
        }
    

    【讨论】:

      【解决方案3】:

      我在另一个帖子中回答了类似的问题。

       func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat{
      
          if indexPath.row == 0 { 
                return firstRowHeight
           }
          else if indexPath.row == 1 { 
                return secondRowHeight 
          }
          else if indexPath.row == 2 { 
                return thirdRowHeight 
          }
          else { 
                return tableView.frame.size.height - firstRowHeight - secondRowHeight - thirdRowHeight
         }
      
      }
      

      【讨论】:

        猜你喜欢
        • 2019-11-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-10-01
        • 2014-07-03
        • 2021-08-14
        • 2013-08-25
        • 2019-06-11
        相关资源
        最近更新 更多