将从NIB加载的视图添加到自定义表格视图单元格不能按预期工作
我一直在重复使用表格视图单元格内的视图。我有一个自定义表格视图单元格和另一个可重用的视图,我在单元格和我的应用程序的其他部分使用了该视图。可重用视图在XIB文件中定义并与其类定义相关联。将从NIB加载的视图添加到自定义表格视图单元格不能按预期工作
我有奇怪的现象,当我试图让视图定制表视图细胞
内DevicesTableViewCell < - 自定义的UITableViewCell持有一种观点认为是在另一个NIB。
DeviceView < - 可以在细胞中和在应用程序的其他部分,如堆栈视图可以使用可重复使用的自定义视图。
现在,当我尝试加载笔尖并将其添加到单元格内容视图,该视图不会出现。但是,如果我尝试将它添加到表视图单元格内的容器视图,它确实有效。
小区建立代码:
let devicesName = sectionDataArray[MasterSection.devices.rawValue].reuseId
let devicesNib = UINib(nibName: devicesName, bundle: nil)
tableView.register(devicesNib, forCellReuseIdentifier: devicesName)
-
代码,其中视图不会出现:
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 }
-
代码,其中视图确实出现:
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
}
}
结果:
添加到内容查看 - >没有显示,但在白色背景。
添加到containerView - >的视图显示为正常,并且包含在与内部正确自动布局约束的细胞。
备注: containerView位于Custom Table Cell View XIB文件中。容器视图具有自动布局约束,将其视图绑定到表视图单元格视图,边距为0.
问题: 任何人都可以解释为什么在这种特殊情况下内容视图不能正确显示其子视图?
问题是我在XIB文件中有错误的对象。
的DevicesTableViewCell.xib内部,创建的视图是一个UIView对象。不是UITableViewCell对象。
如果实例从厦门国际银行的小区,确保视图对象从一个UITableViewCell创建。
这是因为UITableViewCell对象中包含了一个内容视图。
更改XIB文件添加对象到内容视图后工作正确。
我想你的容器视图是隐藏在代码中检查该行deviceCell.containerView.isHidden =真只是试图改变你的代码deviceCell.containerView.isHidden的第一部分=假电池绘图,但容器被隐藏起来的细胞可以” t显示其内容。 – dragoneye
这是正确的,容器视图是隐藏的,因为它坐落在内容视图之上,这就是为什么我添加子视图的内容视图和隐藏容器视图。 –
但内容视图不显示任何内容。在第二种情况下,我将子视图添加到工作的容器视图中。 –