0

How to set spacing between two cells? My code is not working.

UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 3)];/// change size as you need.
separatorLineView.backgroundColor = [UIColor whiteColor];// you can also put image here
[cell.contentView addSubview:separatorLineView];
cell.contentView.backgroundColor = [UIColor colorWithHex:@"#E8E8E8"];
cell.contentView.layer.cornerRadius= 6.0;
//    cell.contentView.layer.shadowOffset = CGSizeMake(-1, 1);
cell.contentView.layer.borderWidth = 1.0f;
[cell setLayoutMargins:UIEdgeInsetsMake(10, 10, 10, 10)];

This is my Layout

James Z
  • 12,209
  • 10
  • 24
  • 44
Vicky Mahale
  • 1,209
  • 11
  • 18
  • you mean like this: https://stackoverflow.com/a/21901250/1361672 – RLoniello Jan 04 '18 at 14:41
  • 1
    Possible duplicate of [How to add spacing between UITableViewCell](https://stackoverflow.com/questions/6216839/how-to-add-spacing-between-uitableviewcell) – RLoniello Jan 04 '18 at 14:42
  • #Ercell0 I already refer this link, but result had display multiple time – Vicky Mahale Jan 04 '18 at 14:44
  • You could just add an intermediate UIView to the content view and then make everything else a sub view of that. Then change that UIView and you get an impression of the cell being smaller when indeed it still takes up the full size. – Upholder Of Truth Jan 04 '18 at 15:41

3 Answers3

0

You can create sections instead of cells, so that you have 1 cell per section.

And then in the TableView delegate method you can use this :

  // Set the spacing between sections
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return cellSpacingHeight
    }
0

This will help you!

write Inside UITableViewCell subclass

override func layoutSubviews() {
    super.layoutSubviews()

    contentView.frame = UIEdgeInsetsInsetRect(contentView.frame, UIEdgeInsetsMake(10, 10, 10, 10))
}
Madhur
  • 1,056
  • 9
  • 14
0

Simplest solution is just add relevant constraint in your cell XIB/Storyboard

MichaelV
  • 1,231
  • 8
  • 10
  • I think you will find you can't create those kind of constraints on the content view of a UITableViewCell. – Upholder Of Truth Jan 04 '18 at 15:30
  • I am prepared to stand (or in my current case sit) corrected but can you show how the constraints were setup to achieve that? – Upholder Of Truth Jan 05 '18 at 12:34
  • I see you have set constraints to inset the BubbleView inside the content view itself but not constraints to inset the content view inside the UITableViewCell. I misunderstood the constraints you were talking about. You are indeed correct about those kind of constraints and it's what I was getting at with my intermediate view comment to the original question. The content view still fills the UITableViewCell. – Upholder Of Truth Jan 05 '18 at 14:37
  • Yes. IMHO it is incorrect to do anything with context view of the cell, it you don't want to hack native UIKit table behaviour, because it is actually context view of the cell, and should represent all available area. Indeed the UIKit handle it changed more the once before. – MichaelV Jan 05 '18 at 15:11
  • Yep I fully agree. Hacking native behaviours can appear to work but then a new version of iOS comes out and everything falls apart. – Upholder Of Truth Jan 05 '18 at 15:13