ios - Swift 自定义 TableViewCell(卡)

标签 ios swift uitableview pure-layout

我正在尝试制作一个类似卡片的单元(看起来像是独立 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/

相关文章:

ios - 如何以正确的方式解析 IOS SWIFT 3 中的 Google Distance Matrix API JSON

ios - 带有 MacOS 的 Delphi 10.4 - 最低版本的 MacOS 和 XCode 可能吗?

ios - Swift 图标角标(Badge)编号感叹号

ios - 从 View Controller 访问变量到覆盖函数

xamarin.ios - 将背景设置为 GroupTableViewBackgroundColor

ios - UITableViewController 行在标题单元格后面滚动?

iphone - Xcode 在较新的 IOS 上使用 3.2

iphone - NSXMLParser 解析使用 Windows-1256 编码的 xml 文件

ios - 带协议(protocol)的动态初始化程序

ios - 搜索时将结果加载到表上