0

我正在尝试制作一张类似卡片的卡片cell(看起来像是自己漂浮的卡片)。现在,我只是想让一个红色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。没有红色视图出现。任何人都知道我缺少的那块拼图(PS 我正在使用 PureLayout)

4

1 回答 1

0

tableView 的 contentView 不能也不响应 margins ,它占用了 tableView 的整个宽度,你还必须使用自动布局来放置 redView 以便它延伸到它的内容,或者heightForRowAt用于框架-布局

于 2018-07-23T00:39:41.300 回答