如何使最后一行uitableview填充空白区域

时间:2016-04-08 13:46:58

标签: ios swift uitableview

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

我的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屏幕尺寸而异。

3 个答案:

答案 0 :(得分:8)

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

首先,将表格视图本身的顶部和底部约束到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

答案 1 :(得分:0)

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

 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
   }

}

答案 2 :(得分:0)

此答案适用于可能存在太多不同高度不同的单元格的情况,因此计算单元格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)

    }