繁体   English   中英

无法将从NIB加载的视图添加到自定义表视图单元中

[英]Adding a view loaded from a NIB to a Custom Table View Cell doesn't work as expected

我一直在重复使用表视图单元格中的视图。 我有一个自定义表格视图单元格,以及在该单元格和应用程序其他部分中使用的另一个可重用视图。 可重用视图在XIB文件中定义,并与它的类定义相对应地链接。

当我尝试在自定义表格视图单元格中获取视图时,我的行为很奇怪

DevicesTableViewCell <-自定义UITableViewCell,用于保存另一个NIB中的视图。

DeviceView <-自定义可重用视图,可以在单元格和应用程序的其他部分中使用,例如堆栈视图。

现在,当我尝试加载笔尖并将其添加到“单元格内容”视图时,该视图不会出现。 但是,如果我尝试将其添加到表视图单元格内的容器视图中,则它确实可以工作。

单元设置代码:

    let devicesName         = sectionDataArray[MasterSection.devices.rawValue].reuseId
    let devicesNib          = UINib(nibName: devicesName, bundle: nil)
    tableView.register(devicesNib, forCellReuseIdentifier: devicesName)
  1. 视图不出现的代码:

     override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: sectionDataArray[indexPath.section].reuseId) print ("-- Showing devices cell") let deviceCell = cell as! DevicesTableViewCell // Optimise this, we way not necessarily need to load this view every single time if deviceCell.deviceView == nil { deviceCell.deviceView = Bundle.main.loadNibNamed("DeviceView", owner: self, options: nil)?.first as! DeviceView deviceCell.containerView.isHidden = true deviceCell.contentView.addSubview(deviceCell.deviceView) let views = ["deviceView" : deviceCell.deviceView] deviceCell.deviceView.translatesAutoresizingMaskIntoConstraints = false deviceCell.contentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[deviceView]|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views)) deviceCell.contentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[deviceView]|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views)) } // TODO - Setup cell data contents return deviceCell } 
  2. 视图确实出现的代码:

     override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: sectionDataArray[indexPath.section].reuseId) print ("-- Showing devices cell") let deviceCell = cell as! DevicesTableViewCell // Optimise this, we way not necessarily need to load this view every single time if deviceCell.deviceView == nil { deviceCell.deviceView = Bundle.main.loadNibNamed("DeviceView", owner: self, options: nil)?.first as! DeviceView //deviceCell.containerView.isHidden = true deviceCell.containerView.addSubview(deviceCell.deviceView) let views = ["deviceView" : deviceCell.deviceView] deviceCell.deviceView.translatesAutoresizingMaskIntoConstraints = false deviceCell.containerView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[deviceView]|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views)) deviceCell.containerView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[deviceView]|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views)) } // TODO - Setup cell data contents return deviceCell } 

自定义单元格代码:

class DevicesTableViewCell: UITableViewCell {

@IBOutlet weak var containerView: UIView!
var deviceView: DeviceView!

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

}

结果:

  1. 添加到contentView->仅显示白色背景。

  2. 添加到containerView->视图将正常显示,并包含在内部具有正确自动布局约束的单元格中。

注意:containerView在“定制表单元格视图” XIB文件中。 容器视图具有自动布局约束,其视图以空白为0绑定到表视图单元格视图。

问题:谁能解释为什么在这种特殊情况下,“内容”视图不能正确显示其子视图?

问题是我在XIB文件中有错误的对象。

在DevicesTableViewCell.xib内部,创建的视图是一个UIView对象。 不是UITableViewCell对象。

如果从XIB实例化单元格,请确保从UITableViewCell创建了视图对象。

这是因为UITableViewCell对象具有包装在其中的内容视图。

更改XIB文件后,将对象添加到内容视图即可正常工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM