How to increase the UITableView separator height?

后端 未结 17 1958
花落未央
花落未央 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:47

    For Swift 4

    Implemented King-Wizard's solution to Swift 4:

    public func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        let additionalSeparatorThickness = CGFloat(4)
    
        let additionalSeparator = UIView(frame: CGRect(x: 0,
                                                       y: cell.frame.size.height - additionalSeparatorThickness, width: cell.frame.size.width, height: additionalSeparatorThickness))
        additionalSeparator.backgroundColor = UIColor.groupTableViewBackground
        cell.addSubview(additionalSeparator)
    
    }
    
    0 讨论(0)
  • 2020-12-08 00:50

    I came across a way that has allowed me to effectively change the gap between cells.

    In Interface builder I set the row height to be 46.

    In the cellForRowAtIndexPath method of my TableView delegate I set the frame of the cell to be a smaller value.

    cell.frame=CGRectMake(44,0,tableView.bounds.size.width,44)
    

    This gives me a cell with a height of 44 that fits the width of the tableView but the space provided for the row will be 46 as defined in IB.

    I was filling the cell programmatically anyway so this suited me fine.

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

    I do it a much simpler and more flexible way. Some may call it a hack. I call it pragmatic.

    I hide the standard UITableViewSeparator. I then add a subview to my cell, using auto layout pin it to the top. Fix the height to what I desire. Pin it to the edges with a margin either side. Change it's background colour. I have a plain separator with the height i desire.

    You may question how efficient this is having another UIView in the cell hierarchy. Is it really going to make a noticeable difference? Probably not - you've just taken the standard separator out of the table hierarchy anyway.

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

    In Swift

    The easiest and shortest way for me was to add the snippet below in cellForRowAtIndexPath or in willDisplayCell:

    override func tableView(tableView: UITableView,
                            willDisplayCell cell: UITableViewCell,
                            forRowAtIndexPath indexPath: NSIndexPath)
    {
            let additionalSeparatorThickness = CGFloat(3)
            let additionalSeparator = UIView(frame: CGRectMake(0,
                                                               cell.frame.size.height - additionalSeparatorThickness,
                                                               cell.frame.size.width,
                                                               additionalSeparatorThickness))
            additionalSeparator.backgroundColor = UIColor.redColor()
            cell.addSubview(additionalSeparator)
    }
    
    0 讨论(0)
  • 2020-12-08 00:55

    You can do this entirely in the storyboard. Here is how:

    • go to the storyboard and select the tableview
    • Show the Size Inspector and from there set row height to say 140.
    • then show the Attributes Inspector and from there set your separator to Single Line and Style Plain and choose a color
    • then in the storyboard (or in Document Outline) select the cell
    • and again in the Size Inspector, under the Table View Cell, set custom Row Height to say 120.

    That’s all. Your separator will be 20 units tall.

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

    I don't think it's possible using standard API. I guess you would need to subclass the UITableViewCell and add a view that simulates a separator at the bottom of the cell.

    You may want to check this question, it seems related and has some sample code: iPhone + UITableView + place an image for separator

    0 讨论(0)
提交回复
热议问题