[英]UITableViewCell margin left and right
從您的屏幕截圖來看,您似乎正在嘗試使用分組表視圖來執行此操作。 要做到這一點,你應該使用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
子類。
你應該得到如下結果:
從 iOS 13.0 開始,有一個新的 tableview 樣式“Inset grouped”可以做到這一點: https : //developer.apple.com/documentation/uikit/uitableview/style/insetgrouped
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.