具有动态子视图堆栈的自定义单元格

时间:2016-06-27 09:21:49

标签: ios swift uitableview tableviewcell

我有一个由'Post'数组填充的tableView。在这个tableView上我注册了一个类'PostCell'。任何笔尖都不支持此PostCell,因为我想用子视图动态组合它。

假设两个子视图,一个顶部的yellowView和一个底部的redView,UIView的每个子类,都堆叠在一个PostCell上。

我的问题是以下代码返回:

enter image description here

的ViewController

override func viewDidLoad(){
  super.viewDidLoad()
  tableView.delegate = self
  tableView.datasource = self
  tableView.rowHeight = UITableViewAutomaticDimension
  tableView.estimatedRowHeight = 80
  tableView.registerClass(PostCell.self, forCellReuseIdentifier: "PostCell")
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  let post = posts[indexPath.row]
  let cell = tableView.dequeueReusableCellWithIdentifier("PostCell") as! PostCell
  cell.configureForData(post)
  return cell 
}

PostCell

class PostCell: UITableViewCell {

  override init(style: UITableViewCellStyle, reuseIdentifier: String?){
    super.init(style: style, reuseIdentifier: reuseIdentifier)
  }

  required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
  }

  fun configureForData(post: Post){
    let yellowView: UIView = UIView(frame: CGRect(x: 0, y: 0, width: contentView.frame.width, height: 40))
    yellowView.backgroundColor = UIColor.yellowColor()
    yellowView.translatesAutoresizingMaskIntoConstraints = false
    contentView.addSubview(yellowView)

    let redView: UIView = UIView(frame: CGRect(x: 0, y: 40, width: contentView.frame.width, height: 40))
    redView.backgroundColor = UIColor.redColor()
    redView.translatesAutoresizingMaskIntoConstraints = false
    contentView.addSubview(redView)
  }

  override func layoutSubviews() {
    super.layoutSubviews()
    let height = NSLayoutConstraint(item: contentView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 80)
    contentView.translatesAutoresizingMaskIntoConstraints = false
    contentView.addConstraint(height)
  }
}

编辑1 :这会修复布局

class PostCell: UITableViewCell {

  let yellowView = UIView()
  let redView = UIView()      

  override init(style: UITableViewCellStyle, reuseIdentifier: String?){
    super.init(style: style, reuseIdentifier: reuseIdentifier)
  }

  required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
  }

  fun configureForData(post: Post){
    yellowView.backgroundColor = UIColor.yellowColor()
    yellowView.translatesAutoresizingMaskIntoConstraints = false
    contentView.addSubview(yellowView)

    redView.backgroundColor = UIColor.redColor()
    redView.translatesAutoresizingMaskIntoConstraints = false
    contentView.addSubview(redView)

    layoutIfNeeded
  }

  override func layoutSubviews() {
    super.layoutSubviews()
    let height = NSLayoutConstraint(item: contentView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 80)
    contentView.translatesAutoresizingMaskIntoConstraints = false
    contentView.addConstraint(height)

    yellowView.frame = UIView(frame: CGRect(x: 0, y: 0, width: contentView.frame.width, height: 40))
    redView.frame = UIView(frame: CGRect(x: 0, y: 40, width: contentView.frame.width, height: 40))
  }

1 个答案:

答案 0 :(得分:1)

尝试在cell.layoutIfNeeded()功能cell.configureForData(post)之后使用tableView:cellForRowAtIndexPath。但我认为最好计算单元格高度并将其返回tableView:heightForRowAtIndexPath函数。

你也有细胞宽度问题:

这是因为你在id正确布局之前得到了contentView.frame.width。您需要更新每个layoutSubviews调用中的帧或使用约束。