ios - 具有动态 subview 堆栈的自定义单元格

标签 ios swift uitableview

我有一个由“Post”数组填充的 tableView。在此 tableView 上,我注册了一个类“PostCell”。任何 Nib 都不支持此 PostCell,因为我想将其与 subview 动态组合。

假设两个 subview ,一个顶部的 YellowView 和一个底部的 RedView,每个 UIView 的子类,都堆叠在 PostCell 上。

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

enter image description here

View Controller

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))
  }

最佳答案

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

你也有单元格宽度的问题:

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

关于ios - 具有动态 subview 堆栈的自定义单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38050188/

相关文章:

ios - iOS读取TXT记录

ios - iMessage 打字动画

ios - 如何使用 alamofire 管理器在 ios app swift4 中添加 ssl 证书固定?

objective-c - Objective-C : How to make background color of UITableView Consistent

ios - 如何清除缓存的 UITableViewCell

ios - 如何将来自 github 的自定义控件 merge 到我的项目中并保持最新

ios - 标题部分 View UITableView 中的标签和按钮

ios - Swift 如何在 IBaction 之外更改按钮标签?

ios - 为什么lazy property和didSet会以递归调用结束?

ios - 如何获取 UITableView 内的 UICollectionView 内的数组数组并返回正确数量的项目?