【问题标题】:UITableViewController AutoLayoutConstraintsUITableViewController AutoLayoutConstraints
【发布时间】:2018-01-29 19:55:15
【问题描述】:

我已经为 UITableViewCell 画了一个边框

我必须为 UITableViewCell 设置前导和尾随约束,以便边框看起来正确。

要绘制边框,我在cellForRowAt(:)使用了以下代码

 let today = tableView.dequeueReusableCell(withIdentifier: "todo", for: indexPath) as! DisplayTODOTaskCell


                today.layer.masksToBounds = true
                today.layer.cornerRadius = 0
                today.layer.borderWidth = 1
                today.layer.shadowOffset = CGSize(width: -1, height: 1)
                let borderColor: UIColor = .black
                today.layer.borderColor = borderColor.cgColor

当我使用下面的代码时,我遇到了运行时错误。

today.constraints(NSLayoutConstraint(item: today, attribute: .alignAllLeading, relatedBy: nil, toItem: self, attribute: self, multiplier: 1, constant: 10))

enter image description here

当我从手机查看时左右边框不可见。

我用过 TableViewController。

我可以通过留出空间来绘制单元格的边框。

需要我为不同部分的后续单元格实现,为此我需要知道上一个部分的高度。请告诉我如何实现这一点

我在 UITableViewCell 类中添加了以下代码。 覆盖 func layoutSubviews() { self.frame = CGRect(x: 10.0, y: 5.0, width: 390.0, height: self.frame.size.height) print("1111 高度 = (self.frame.size.height)") super.layoutSubviews() }

【问题讨论】:

  • 您需要出示您的代码,以便我们为您提供指导。到目前为止,您尝试过什么?

标签: ios swift uitableview autolayout


【解决方案1】:

不要直接自定义Cell,而是在UITableViewCell内部创建UIView

使用StoryboardDisplayTODOTaskCell 中添加UIView。给出约束,{top 2, left 5, bottom 2, right 5}。

在这个 UIView 中,你可以设计任何你需要的东西。

tableviewcell backgroundcolorwhite

  let today = tableView.dequeueReusableCell(withIdentifier: "todo", for: indexPath) as! DisplayTODOTaskCell


            today.createdView.layer.masksToBounds = true
            today.createdView.layer.cornerRadius = 0
            today.createdView.layer.borderWidth = 1
            today.createdView.layer.shadowOffset = CGSize(width: -1, height: 1)
            let borderColor: UIColor = .black
            today.createdView.layer.borderColor = borderColor.cgColor

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-07
    • 2014-02-07
    • 2019-03-03
    • 2012-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多