Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift自定义TableViewCell(卡)_Ios_Swift_Uitableview_Pure Layout - Fatal编程技术网

Ios Swift自定义TableViewCell(卡)

Ios Swift自定义TableViewCell(卡),ios,swift,uitableview,pure-layout,Ios,Swift,Uitableview,Pure Layout,我正在尝试制作一张像cell一样的卡片(一张看起来像是自己在浮动的卡片)。现在,我只是想在单元格中显示一个红色的UIView(边距用12像素填充)。以下是来自UITableViewCell类的代码: import UIKit import PureLayout class MovieTableViewCell: UITableViewCell { let cardView = UIView() override init(style: UITableViewCellStyl

我正在尝试制作一张像
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")
    }
}

目前我得到一个空白的
表格视图
。没有显示红色视图。任何人都知道我缺少的拼图部分(请注意,我使用的是PureLayout)

tableView的contentView不能也不响应边距,它占据了tableView的整个宽度,您还必须使用auto layout来放置redView,使其延伸到其内容,或者使用

heightForRowAt
进行框架布局