Augusto
Augusto

Reputation: 4253

How to set padding/margin and corner radius to TableViewCell of TableView?

I've UITableView and I wants set corner radius and a margin/padding to Top/Right/Left/Bottom. But, if I try set this, my layout is messing. And, when I try: cell.cellView.layer.cornerRadius = 20.0 fires nil pointer exception.

My layout is like this:

enter image description here

A example of I like that my layout be:

enter image description here

I wants that my cells stay "floating" with corner radius and margins, without mess my layout builded.

Also about UITableView, how I remove the unused cells? I have only 3 cells, but TableView shows all cell possibles, leaving others cell not useds.

enter image description here

Also (4): Why my cells appears united? This not appears "floating"

enter image description here

Upvotes: 3

Views: 4919

Answers (2)

Kuldeep
Kuldeep

Reputation: 4552

For your require layout do below steps.

  1. Add UIView in you cell contentView with Leading, Top, Trailing, Bottom Constraint. Like (15, 10, 15, 10)

  2. Select that UIView and check Clip to Bounds checkmark.

  3. Add your required UI inside that UIView.

  4. Set UIView cornerRadius inside layoutSubviews() inside your cell file

And to remove UnUsed cell write this line your viewDidLoad -> self.tblVW.tableFooterView = UIView.init(frame: .zero)

Here is demo Link: https://www.dropbox.com/s/815loyp20r0oetf/Demo.zip?dl=0

Upvotes: 2

Roshan
Roshan

Reputation: 732

I am assuming you are taking a view inside table view cell on which your all content is present. that is why your using cell.cellView

what you need here is that just add this line too if in case you are missing.

cell.cellView.clipToBounds = true

This should work.

In case you are not taking an UIView inside cell content view then follow this steps.

  • Take a UIView from object library.
  • give it required margins inside cell content view.
  • add all your required UI components in this view.
  • set clipToBounds to true along with cornerRadius

Upvotes: 0

Related Questions