2016-08-02 69 views
2

我想製作這樣的單元格。我使用迅速。UITableViewCell保證金左右

但我不知道如何在單元的右側和左側添加邊距。

你知道如何做同樣的邊緣效應嗎? (如果有多個細胞,接觸的邊緣不四捨五入) enter image description here

當我使用contentInset:

self.tableView.contentInset = UIEdgeInsetsMake(0, -15, 0, 0) 

enter image description here

self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, -15) 

enter image description here

+0

試試這個'self.tableView.contentInset = UIEdgeInsetsMake(0,-15,0,-15)' –

+0

它工作在正確的但不是在左... –

+0

自定義座標,當然工作 –

回答

4

從你的屏幕截圖它看起來像你試圖用分組表格視圖做到這一點。要做到這一點,你應該使用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 
     } 
    } 

您只需根據行的索引路徑和節中的行數應用掩碼。如果您有動態調整大小的單元格,則可能需要將掩碼應用到UITableViewCell子類。

你應該得到這樣的結果:

enter image description here