Home > Software design >  adding padding between two cells in TableView
adding padding between two cells in TableView

Time:06-06

created a basic chat application using websocket, and for a proper look I want to give my each cell a little bit padding. But really couldn't figure out how to do that currently tried below solution but it only gives padding "inside my cell" which I want "outside padding"

tried

import UIKit

class CellView: UITableViewCell {

    override func awakeFromNib() {
        super.awakeFromNib()
        super.layoutSubviews()
        self.backgroundColor = .systemBlue
        self.layer.cornerRadius = 10
       
      
      
    }
    override func layoutSubviews() {
        super.layoutSubviews()

        contentView.frame = contentView.frame.inset(by: UIEdgeInsets(top: 8, left: 8, bottom: 8, right: 8))
    }
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

tried

func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return CGFloat(15.0)
    }
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let v = UIView()
        v.backgroundColor = .clear
        return v
    }

CodePudding user response:

There is no such concept as the space between cells in UITableView.

Your best bet is to add padding to the content of the cell itself.

So, instead of having a cell of height 80 with content height 80. Change the row height to 100 and keep the content at height 80.

By doing that you now have created a “space” of 20 between each cell.

  • Related