Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make last row of uitableview fill the empty space

I would like the last cell/row (mapView) to fill the empty space below. enter image description here

My tableviewcontroller code

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
        }
    }

}

Sometimes I will have more rows to displays before the map. So the empty space below can vary. Of cours the empty space will also vary with iPhone screen size.

like image 748
elyrico Avatar asked Apr 08 '16 13:04

elyrico


2 Answers

You need to figure out how much empty space you have, and then set the height of your map cell to that.

First, constraint the top and bottom of the table view itself to the superview, to ensure it occupies the whole screen.

The visible height of your table view will be:

    self.tableView.bounds.size.height

In heightForRowAtIndexPath, calculate the height of the cells above the map cell, and return the difference between that and the visible height of the table view:

    let otherCellsHeight: CGFloat = fakeArray.count * 44

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

Of course, you should also check that the calculated height of the map cell is not negative or smaller than some minimum height, just to be safe:

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

    return (availableSpace > 80) ? availableSpace : 80
like image 84
Mike Taverne Avatar answered Sep 30 '22 02:09

Mike Taverne


This answer is for cases where there may be too many different cells with too many different heights, so calculating the height of the cells 0..n-1 is not as easy as in @Mike Taverne's answer.

In my case the last cell should expand vertically but there is a minimal height is should have so it looks good. In that case the tableView is scrollable.

Here is the code:

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)

    }
like image 23
regina_fallangi Avatar answered Sep 30 '22 01:09

regina_fallangi