在iOS開發中,UITableView是一個非常常見的元件,它可以讓開發者快速的建立一個列表,但是有時候,我們會想要自定義UITableView的分割線,來增加我們的App的美觀度,那麼在Swift中,我們又該如何自定義UITableView的分割線呢?

在Swift中,我們可以使用UITableViewDelegate來自定義UITableView的分割線,該Delegate有一個方法叫做tableView(_:willDisplayCell:forRowAt:),該方法會在每個Cell顯示之前被調用,我們可以在該方法中自定義UITableView的分割線,以下是一個簡單的範例:

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    let separatorLine = UIBezierPath(rect: CGRect(x: 0, y: cell.frame.height - 1, width: cell.frame.width, height: 1))
    let separatorLayer = CAShapeLayer()
    separatorLayer.path = separatorLine.cgPath
    separatorLayer.fillColor = UIColor.red.cgColor
    cell.layer.addSublayer(separatorLayer)
}

上面的程式碼會在每個Cell顯示之前,將一個紅色的分割線加入到Cell的Layer中,以此來自定義UITableView的分割線,這樣就可以讓我們的App更加美觀了。

推薦閱讀文章

推薦閱讀文章

Swift UITableView 分割線 自定義分割線 🎨
Customize UITableView Separator in Swift 3
How to customize the separator between table view cells
Customize UITableView Separator in Swift
UITableView Tutorial for Beginners</a

延伸閱讀本站文章

更多swift相關文章

Swift UITableView 分割線 自定義分割線 🎨

Categorized in:

Tagged in:

,