Ebin Joy
Ebin Joy

Reputation: 3189

Add space below last element of Table view in swift

I need to add some extra space after the last element in the table view cell.

In android reccycler view, the same thing can be achieved by

android:paddingBottom="8dp"
 android:clipToPadding="false"

Table view last item

Upvotes: 5

Views: 3378

Answers (6)

MALIKK HABIB UR REHMAN
MALIKK HABIB UR REHMAN

Reputation: 880

Swift 5

I tried adding tableView inset at first but it didn't work in my case. Rather I tried adding footerView at the end of UItableView.

    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let footerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 100))
        return footerView
    }

// Incase you have more then one sections. you will add at the end of last section.

    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        if section == exercisesArray.count-1 {
            return 60
        }else{
            return 0
        }
        
    }

Upvotes: 2

gavin olsen
gavin olsen

Reputation: 21

Going off of a previous answer, you need to check if it's the last cell.

let bottomPadding = 8

if (self?.tableView.numberOfRows(inSection: 0) ?? 0) - 1 == row {
    let insets = UIEdgeInsets(top: 0, left: 0, bottom: bottomPadding, right: 0)
    strongSelf.tableView.contentInset = insets
}

Assuming you're editing the first section.

Upvotes: 0

Manoj Saini
Manoj Saini

Reputation: 21

Add tableFooterView in tableview.

let tableFooterView  = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 20))

  tableFooterView.backgroundColor = UIColor.white

  tableView.tableFooterView = tableFooterView

Upvotes: 2

Niilesh R Patel
Niilesh R Patel

Reputation: 707

When you use content inset for add padding it may create some issue, when no data available in tableView.

Try this

Simply and the view in a tableFooterView for the padding in the bottom of tableView.

//Add Padding in the bottom of tableview

let view = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 20))
view.backgroundColor = UIColor.white
tableView.tableFooterView = view

Upvotes: 4

Shubham Bakshi
Shubham Bakshi

Reputation: 572

You need to add insets to your tableView . Try the following code

let insets = UIEdgeInsets(top: 0, left: 0, bottom: 50, right: 0)
tableView.contentInset = insets

Upvotes: 15

Rizwan
Rizwan

Reputation: 3666

Add datasource and delegate for your tableview and utilize following delegate methods: - heightForFooterInSection & viewForFooterInSection

// set view for footer
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    let footerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 40)) // assuming 40 height for footer.
    footerView.backgroundColor = <Some Color>
    return footerView
}

// set height for footer
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return 40
}

Upvotes: 4

Related Questions