我一直在重複使用表格視圖單元格內的視圖。我有一個自定義表格視圖單元格和另一個可重用的視圖,我在單元格和我的應用程序的其他部分使用了該視圖。可重用視圖在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.
問題: 任何人都可以解釋爲什麼在這種特殊情況下內容視圖不能正確顯示其子視圖?
我想你的容器視圖是隱藏在代碼中檢查該行deviceCell.containerView.isHidden =真只是試圖改變你的代碼deviceCell.containerView.isHidden的第一部分=假電池繪圖,但容器被隱藏起來的細胞可以」 t顯示其內容。 – dragoneye
這是正確的,容器視圖是隱藏的,因爲它坐落在內容視圖之上,這就是爲什麼我添加子視圖的內容視圖和隱藏容器視圖。 –
但內容視圖不顯示任何內容。在第二種情況下,我將子視圖添加到工作的容器視圖中。 –