How to increase the UITableView separator height?

后端 未结 17 1959
花落未央
花落未央 2020-12-08 00:19

I want more space(10px) between each cell. How can I do this?

And I have added this code

tableView.separatorStyle = UITableViewCellSepar         


        
相关标签:
17条回答
  • 2020-12-08 00:57

    You should implement

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    

    delegate method. and return 100.0 there.

    0 讨论(0)
  • 2020-12-08 00:59

    The easier and safest solution to this problem is to turn off the table separator and use a UITableViewCell as a separator of variable height. Sure, you'll have to do some index math to figure out where items are, but really it's odd / even.

    It won't break and you get the benefit of recyclable cells (no extraneous views to clean up).

    0 讨论(0)
  • 2020-12-08 01:00

    The best way for me, just add this in cellForRowAtIndexPath or in willDisplayCell

    CGRect sizeRect = [UIScreen mainScreen].applicationFrame;    
    NSInteger separatorHeight = 3;
    UIView * additionalSeparator = [[UIView alloc] initWithFrame:CGRectMake(0,cell.frame.size.height-separatorHeight,sizeRect.size.width,separatorHeight)];
    additionalSeparator.backgroundColor = [UIColor grayColor];
    [cell addSubview:additionalSeparator];
    

    For Swift 3.0:

    let screenSize = UIScreen.main.bounds
    let separatorHeight = CGFloat(3.0)
    let additionalSeparator = UIView.init(frame: CGRect(x: 0, y: self.frame.size.height-separatorHeight, width: screenSize.width, height: separatorHeight))
    additionalSeparator.backgroundColor = UIColor.gray
    self.addSubview(additionalSeparator)
    

    You should add this to cell's method awakeFromNib() to avoid re-creation.

    0 讨论(0)
  • 2020-12-08 01:02

    this is quite old. Nevertheless I will post my approach.

    Simply increase your cell height a bit and assign a mask layer to the cell, like that:

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "...", for: indexPath)
    
        // Configure the cell...
        let maskLayer = CAShapeLayer()
        let bounds = cell.bounds
        maskLayer.path = UIBezierPath(roundedRect: CGRect(x: 2, y: 2, width: bounds.width-4, height: bounds.height-4), cornerRadius: 5).cgPath
        cell.layer.mask = maskLayer
    
        return cell
    }
    

    So in this example my seperator height will be 4.

    Have fun!

    0 讨论(0)
  • 2020-12-08 01:02

    This is the easiest solution I've found:

    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        " "
    }
    

    then just set the height to whatever you want:

    tableView.sectionHeaderHeight = 30.0
    
    0 讨论(0)
提交回复
热议问题