UITableViewCell左右边距

Hug*_*uet 3 margin uitableview ios swift

我想做这样的细胞。我用迅捷。

但是我不知道如何在单元格的右侧和左侧添加边距。

您知道如何对截面进行相同的边缘效果吗?(当有多个单元格时,接触的边缘不会变圆) 在此处输入图片说明

当我使用contentInset时:

self.tableView.contentInset = UIEdgeInsetsMake(0, -15, 0, 0)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, -15)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

bey*_*ulf 5

从屏幕快照中,您似乎正尝试使用分组表格视图来执行此操作。为此,您应该使用UITableView加号而UIViewController不是UITableViewController

要设置插图,您只需将表格视图的约束/框架设置为从左右边缘稍微进入,然后将视图的背景色设置为 UIColor.groupTableViewBackgroundColor()

然后,cellForRowAtIndexPath您可以说类似:

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cornerRadius:CGFloat = 5.0

        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)

        // Configure your cell

        let sectionCount = tableView.numberOfRowsInSection(indexPath.section)
        let shapeLayer = CAShapeLayer()
        cell.layer.mask = nil
        if sectionCount > 1
        {

            switch indexPath.row {
            case 0:
                var bounds = cell.bounds
                bounds.origin.y += 1.0
                let bezierPath = UIBezierPath(roundedRect: bounds, byRoundingCorners: [.TopLeft, .TopRight], cornerRadii: CGSize(width: cornerRadius,height: cornerRadius))
                shapeLayer.path = bezierPath.CGPath
                cell.layer.mask = shapeLayer
            case sectionCount - 1:
                var bounds = cell.bounds
                bounds.size.height -= 1.0
                let bezierPath = UIBezierPath(roundedRect: bounds, byRoundingCorners: [.BottomLeft, .BottomRight], cornerRadii: CGSize(width: cornerRadius,height: cornerRadius))
                shapeLayer.path = bezierPath.CGPath
                cell.layer.mask = shapeLayer
            default:
                break
            }
            return cell
        }
        else
        {
            let bezierPath = UIBezierPath(roundedRect: CGRectInset(cell.bounds,0.0,2.0), cornerRadius: cornerRadius)
            shapeLayer.path = bezierPath.CGPath
            cell.layer.mask = shapeLayer
            return cell
        }
    }
Run Code Online (Sandbox Code Playgroud)

您只需根据行的索引路径和该节中的行数应用蒙版。如果您具有动态调整大小的单元格,则可能需要将蒙版应用于UITableViewCell子类。

您应该得到如下结果:

在此处输入图片说明