テーブルビューのセル内でビューを再利用して遊んでいます。私はカスタムテーブルビューのセルと私がセルと私のアプリの他の部分で使用する別の再利用可能なビューを持っています。再利用可能なビューはXIBファイルで定義され、それに応じてクラス定義とリンクされます。別のNIBであるビューを保持するために、カスタムのUITableViewCell -NIBからロードされたビューをカスタムテーブルビューのセルに追加すると、期待通りに動作しません。
私はカスタムテーブルビューセル内のビューを取得しようとすると、私は奇妙な行動が生じています
DevicesTableViewCell <。
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
}
}
結果:
がcontentViewに追加 - >何も表示されますが、白の背景。
コンテナビューに追加 - >ビューは通常通り表示され、内部に正しい自動レイアウト制約が設定されたセルに含まれています。
メモ: コンテナビューは、カスタムテーブルセルビューXIBファイルにあります。コンテナビューには、マージンが0のテーブルビューのセルビューへのビューの境界を設定する自動レイアウト制約があります。
質問: この特定のケースでは、なぜコンテンツビューでサブビューが正しく表示されないのですか?
その内容を示す。 – dragoneye
これは正しいです、コンテナビューはコンテンツビューの上にあるため隠されています。これがコンテンツビューにサブビューを追加してコンテナビューを非表示にする理由です。 –
しかし、コンテンツビューには何も表示されません。 2番目のケースでは、動作するコンテナビューにサブビューを追加します。 –