2016-12-16 1 views
0

テーブルビューのセル内でビューを再利用して遊んでいます。私はカスタムテーブルビューのセルと私がセルと私のアプリの他の部分で使用する別の再利用可能なビューを持っています。再利用可能なビューは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) 
  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. コンテナビューに追加 - >ビューは通常通り表示され、内部に正しい自動レイアウト制約が設定されたセルに含まれています。

メモ: コンテナビューは、カスタムテーブルセルビューXIBファイルにあります。コンテナビューには、マージンが0のテーブルビューのセルビューへのビューの境界を設定する自動レイアウト制約があります。

質問: この特定のケースでは、なぜコンテンツビューでサブビューが正しく表示されないのですか?

+0

その内容を示す。 – dragoneye

+0

これは正しいです、コンテナビューはコンテンツビューの上にあるため隠されています。これがコンテンツビューにサブビューを追加してコンテナビューを非表示にする理由です。 –

+0

しかし、コンテンツビューには何も表示されません。 2番目のケースでは、動作するコンテナビューにサブビューを追加します。 –

答えて

1

問題は、XIBファイル内に間違ったオブジェクトがあることでした。

DevicesTableViewCell.xibの内部に作成されたビューは、UIViewオブジェクトでした。 UITableViewCellオブジェクトではありません。

XIBからセルをインスタンス化する場合は、ビューオブジェクトがUITableViewCellから作成されていることを確認してください。

これは、UITableViewCellオブジェクトに内容ビューがラップされているためです。

XIBファイルを変更した後、オブジェクトをコンテンツビューに追加すると正しく動作しました。私はあなたのコンテナビューは、コードチェックのあなたの最初の部分に隠されている。このライン真deviceCell.containerView.isHidden =は自分のコードを変更しようdeviceCell.containerView.isHidden = falseのセルが「描いているが、細胞ができるよう、コンテナが隠されていると思います

関連する問題