Ios 将从NIB加载的视图添加到自定义表视图单元格不会';I don’我没有按预期工作

Ios 将从NIB加载的视图添加到自定义表视图单元格不会';I don’我没有按预期工作,ios,swift,uitableview,subview,tableviewcell,Ios,Swift,Uitableview,Subview,Tableviewcell,我一直在玩重用表视图单元格中的视图的游戏。我有一个自定义表视图单元格和另一个可重用视图,我在单元格和我的应用程序的其他部分中使用。可重用视图在XIB文件中定义,并相应地与其类定义链接 当我试图在自定义表视图单元格中获取视图时,我有一种奇怪的行为 DeviceTableViewCell问题是我在XIB文件中有错误的对象 在DeviceTableViewCell.xib中,创建的视图是UIView对象。不是UITableViewCell对象 如果从XIB实例化单元格,请确保从UITableViewC

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

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


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

在DeviceTableViewCell.xib中,创建的视图是UIView对象。不是UITableViewCell对象

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

这是因为UITableViewCell对象中包含一个内容视图


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

我认为您的容器视图隐藏在代码的第一部分检查此行deviceCell.containerView.isHidden=true只需尝试更改代码deviceCell.containerView.isHidden=false单元格正在绘制,但容器已隐藏,因此单元格无法显示其属性内容。没错,容器视图是隐藏的,因为它位于内容视图的顶部,这就是为什么我向内容视图添加子视图并隐藏容器视图。但是内容视图没有显示任何内容。在第二种情况下,我将子视图添加到容器视图中,这样可以工作。但我认为您正在containerView中显示内容,当containerView隐藏时,它的所有子视图都会自动显示hidden@dragoneye我发现了问题。XIB文件设置错误,请检查我的答案。请投票。:)获取包含contentView的正确xib的最简单方法是在Xcode中从头创建UITableViewCell类,并选中“还创建xib文件”选项。
    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
}

}