spacing between UITableViewCells swift3
Asked Answered
M

7

7

I am creating a IOS app in swift and want to add spacing between cells like this

enter image description here

I would like to give space of each table view cell same like my attach image. How I can do that? and Right now all cells are coming without any space. swift3

Mesoblast answered 4/12, 2017 at 6:28 Comment(3)
What you have tried so far?Declared
i dnt knw how can i solve it thats why for posting here - dahiya_boyMesoblast
chck my answer. to get this demo give me your email id.Declared
T
12

you can try this in your class of tableView cell:

class cell: UITableViewCell{
override var frame: CGRect {
    get {
        return super.frame
    }
    set (newFrame) {
        var frame =  newFrame
        frame.origin.y += 4
        frame.size.height -= 2 * 5
        super.frame = frame
    }
  }
}
Trocki answered 4/12, 2017 at 7:39 Comment(1)
@Ankit Kumawat let me know if found this answer wrong/right!Trocki
O
7

Update: UIEdgeInsetsInsetRect method is replaced now you can do it like this

contentView.frame = contentView.frame.inset(by: margins)

Swift 4 answer:

in your custom cell class add this function

override func layoutSubviews() {
    super.layoutSubviews()
    //set the values for top,left,bottom,right margins
            let margins = UIEdgeInsets(top: 0, left: 0, bottom: 10, right: 0)
    contentView.frame = UIEdgeInsetsInsetRect(contentView.frame, margins)
}

You can change values as per your need

***** Note ***** calling super function

super.layoutSubviews()

is very important otherwise you will get into strange issues

Orizaba answered 18/9, 2018 at 11:38 Comment(0)
D
6
  1. From Storyboard, your view hierarchy should be like this. View CellContent (as highlighted) will contain all the components.

enter image description here

  1. Give margin to View CellContent of 10px from top, bottom, leading & trailing from its superview.

  2. Now, select the tblCell and change the background color.

enter image description here

enter image description here

  1. Now run your project, make sure delegate and datasource are properly binded.

OUTPUT

enter image description here

NOTE: I just added 1 UILabel in View CellContent for dummy purpose.

Declared answered 4/12, 2017 at 7:0 Comment(0)
S
3

If you are using UITableViewCell to achieve this kind of layout, there is no provision to provide spacing between UITableViewCells.

Here are the options you can choose:

  1. Create a custom UIView within UITableViewCell with clear background, so that it appears like the spacing between cells.

enter image description here

You need to set the background as clear of: cell, content view.

  1. You can use UICollectionView instead of UITableView. It is much more flexible and you can design it the way you want.

Let me know if you want any more details regarding this.

Stipend answered 4/12, 2017 at 6:51 Comment(3)
i already design custom UIView within UITableViewCell with clear background but not appears the spacing between cellsMesoblast
Can you show the view hierarchy? And the output that you are gettingStipend
Updated the answer.Stipend
U
2

One simple way is collection view instead of table view and give cell spacing to collection view and use

func collectionView(_ collectionView: UICollectionView,
                    layout collectionViewLayout: UICollectionViewLayout,
                    sizeForItemAtIndexPath indexPath: IndexPath) -> CGSize {
    let widthSize = collectionView.frame.size.width / 1
    return CGSize(width: widthSize-2, height: widthSize+20)
}

And if you want tableview only then add background view as container view and set background color white and cell background color clear color set backround view of cell leading, trilling, bottom to 10

backgroundView.layer.cornerRadius = 2.0
backgroundView.layer.masksToBounds = false
backgroundView.layer.shadowColor = UIColor.black.withAlphaComponent(0.2).cgColor
Upshot answered 4/12, 2017 at 6:45 Comment(1)
sorry for that but your answer not correct not helpful - 234 ShkMesoblast
P
0

Please try it. It is working fine for me.

You can use section instead of row. You return array count in numberOfSectionsInTableView method and set 1 in numberOfRowsInSection delegate method

Use [array objectAtIndex:indexPath.section] in cellForRowAtIndexPath method.

Set the heightForHeaderInSection as 40 or according to your requirement.

Thanks,Hope it will helps to you

Petula answered 4/12, 2017 at 11:45 Comment(1)
What will you do if you have to handle multiple sectionsOrizaba
G
0

- Statically Set UITableViewCell Spacing - Swift 4 - Not Fully Tested.

Set your tableView Row height to whatever value you prefer.

 override func viewDidLoad() {
    super.viewDidLoad()
    tableView.estimatedRowHeight = <Your preferred cell size>
    tableView.rowHeight = UITableViewAutomaticDimension
    // make sure to set your TableView delegates
    tableView.dataSource = self
    tableView.delegate = self
}

 extension YourClass : UITexFieldDelegate, UITextFieldDataSource {
    //Now set your cells.
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell : UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "yourCell", for: indexPath) as! UITableViewCell

        //to help see the spacing.
        cell.backgroundColor = .red
        cell.textLabel?.text = "Cell"

        return cell
   }
   //display 3 cells
   func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 3
   }

   //now lets insert a headerView to create the spacing we want. (This will also work for viewForHeaderInSection)
   func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

    //you can create your own custom view here
    let view = UIView()
    view.frame = CGRect(x: 0, y: 0, width: tableView.frame.width, height: 44) //size of a standard tableViewCell

    //this will hide the headerView and match the tableView's background color.
    view.backgroundColor = UIColor.clear
    return view
 }

  func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 44
  }
}
Grosberg answered 14/3, 2018 at 15:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.