在UITableViewCell中绘制一条线可以通过以下步骤实现:
class LineTableViewCell: UITableViewCell {
var lineView: UIView!
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
lineView = UIView()
lineView.backgroundColor = UIColor.lightGray
contentView.addSubview(lineView)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
lineView.frame = CGRect(x: 0, y: contentView.bounds.height - 1, width: contentView.bounds.width, height: 1)
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "LineTableViewCell", for: indexPath) as! LineTableViewCell
// 设置其他内容
// 根据需要显示或隐藏线条
if indexPath.row == tableView.numberOfRows(inSection: indexPath.section) - 1 {
cell.lineView.isHidden = true
} else {
cell.lineView.isHidden = false
}
return cell
}
这样,每个UITableViewCell都会在底部绘制一条灰色的线条,除了最后一行。你可以根据需要调整线条的颜色、宽度和位置。
领取专属 10元无门槛券
手把手带您无忧上云