UITableViewController AutoLayoutConstraints

时间:2018-01-29 19:55:15

标签: ios swift uitableview autolayout

我为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类中添加了以下代码。       override func layoutSubviews(){            self.frame = CGRect(x:10.0,y:5.0,width:390.0,height:self.frame.size.height)             打印(" 1111高度=(self.frame.size.height)")             super.layoutSubviews()         }

1 个答案:

答案 0 :(得分:0)

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

使用UIViewDisplayTODOTaskCell内添加Storyboard。给出约束,{top 2,left 5,bottom 2,right 5}。

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

tableviewcell backgroundcolor改为white

  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