我正在尝试制作一个类似卡片的单元
(看起来像是独立 float 的)。现在,我只是想让一个红色的 UIView 出现在单元格中(边缘填充 12 像素)。以下是 UITableViewCell
类的代码:
import UIKit
import PureLayout
class MovieTableViewCell: UITableViewCell {
let cardView = UIView()
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
var margins = layoutMargins
margins.left = 12
margins.top = 12
margins.right = 12
margins.bottom = 12
contentView.layoutMargins = margins
cardView.backgroundColor = UIColor.red
contentView.addSubview(cardView)
contentView.backgroundColor = nil
contentView.preservesSuperviewLayoutMargins = false
cardView.autoPinEdgesToSuperviewMargins()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
目前我得到一个空白的tableView
。没有出现红色 View 。任何人都知道我缺少的拼图(P.S.我正在使用 PureLayout)
最佳答案
tableView 的 contentView 不能也不响应边距,它占据了 tableView 的整个宽度,而且您还必须使用自动布局来放置 redView,使其拉伸(stretch)到其内容,或者使用 heightForRowAt
用于框架布局
关于ios - Swift 自定义 TableViewCell(卡),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51470227/