從你的屏幕截圖它看起來像你試圖用分組表格視圖做到這一點。要做到這一點,你應該使用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
子類。
你應該得到這樣的結果:
試試這個'self.tableView.contentInset = UIEdgeInsetsMake(0,-15,0,-15)' –
它工作在正確的但不是在左... –
自定義座標,當然工作 –