尽管已正确初始化,XIB 视图仍未显示任何内容
XIB view doesn't show anything despite being correctly initialized
我正在尝试使用 .xib 文件在 TableViewCell 中制作可重用视图。我将所有需要的数据提供给 TableViewCell,它有一个 UIView,它是我的可重用 .xib。我在 UIView 中正确打印了 init 函数,但模拟器屏幕上没有显示任何内容。找不到原因,卡了好久。
这是我的 xib 文件:
哪个与我的 UIView 正确关联:
import Kingfisher
class DebateBoxView: UIView {
@IBOutlet var debateBoxView: UIView!
@IBOutlet weak var debateBoxImage: UIImageView!
@IBOutlet weak var debateBoxTitle: UILabel!
var debate: Debate! {
didSet {
debateBoxImage.kf.setImage(with: URL(string: debate.imageURL))
debateBoxTitle.text = debate.name
}
}
override init(frame: CGRect){
super.init(frame: frame)
self.commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.commonInit()
}
func commonInit(){
let bundle = Bundle(for: LogoraApp.self) // I'm not using "Bundle.main" because it is a framework.
bundle.loadNibNamed("DebateBoxView", owner: self, options: nil)
debateBoxView.translatesAutoresizingMaskIntoConstraints = false
self.addSubview(debateBoxView)
debateBoxView.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
debateBoxView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
debateBoxView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
debateBoxView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
}
}
在我的 tableViewCell 中:
import UIKit
class DebateBox: UITableViewCell {
@IBOutlet weak var debateBoxView: DebateBoxView!
}
然后在 tableView 本身中:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = searchResultsTableView.dequeueReusableCell(withIdentifier: "searchDebateBox", for: indexPath) as! DebateBox
cell.debateBoxView.debate = debateSearchResults[indexPath.row]
return cell
}
我遇到了类似的问题...尝试在 viewDidLoad 中注册单元格。
searchResultsTableView.register(UINib(nibName: "DebateBox", bundle: nil), forCellReuseIdentifier: "searchDebateBox")
我终于找到了解决办法。问题是我对 xib 文件的约束不够详细,所以它没有显示在 UI 中。如果有人经过,这是我初始化笔尖的代码:
override init(frame: CGRect){
super.init(frame: frame)
self.commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.commonInit()
}
func commonInit(){
let bundle = Bundle(for: Self.self)
let nib = UINib(nibName: String(describing: type(of: self)), bundle: bundle)
nib.instantiate(withOwner: self, options: nil)
addSubview(contentView)
contentView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
contentView.topAnchor.constraint(equalTo: topAnchor),
contentView.bottomAnchor.constraint(equalTo: bottomAnchor),
contentView.leadingAnchor.constraint(equalTo: leadingAnchor),
contentView.trailingAnchor.constraint(equalTo: trailingAnchor)
])
}
我正在尝试使用 .xib 文件在 TableViewCell 中制作可重用视图。我将所有需要的数据提供给 TableViewCell,它有一个 UIView,它是我的可重用 .xib。我在 UIView 中正确打印了 init 函数,但模拟器屏幕上没有显示任何内容。找不到原因,卡了好久。
这是我的 xib 文件:
哪个与我的 UIView 正确关联:
import Kingfisher
class DebateBoxView: UIView {
@IBOutlet var debateBoxView: UIView!
@IBOutlet weak var debateBoxImage: UIImageView!
@IBOutlet weak var debateBoxTitle: UILabel!
var debate: Debate! {
didSet {
debateBoxImage.kf.setImage(with: URL(string: debate.imageURL))
debateBoxTitle.text = debate.name
}
}
override init(frame: CGRect){
super.init(frame: frame)
self.commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.commonInit()
}
func commonInit(){
let bundle = Bundle(for: LogoraApp.self) // I'm not using "Bundle.main" because it is a framework.
bundle.loadNibNamed("DebateBoxView", owner: self, options: nil)
debateBoxView.translatesAutoresizingMaskIntoConstraints = false
self.addSubview(debateBoxView)
debateBoxView.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
debateBoxView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
debateBoxView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
debateBoxView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
}
}
在我的 tableViewCell 中:
import UIKit
class DebateBox: UITableViewCell {
@IBOutlet weak var debateBoxView: DebateBoxView!
}
然后在 tableView 本身中:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = searchResultsTableView.dequeueReusableCell(withIdentifier: "searchDebateBox", for: indexPath) as! DebateBox
cell.debateBoxView.debate = debateSearchResults[indexPath.row]
return cell
}
我遇到了类似的问题...尝试在 viewDidLoad 中注册单元格。
searchResultsTableView.register(UINib(nibName: "DebateBox", bundle: nil), forCellReuseIdentifier: "searchDebateBox")
我终于找到了解决办法。问题是我对 xib 文件的约束不够详细,所以它没有显示在 UI 中。如果有人经过,这是我初始化笔尖的代码:
override init(frame: CGRect){
super.init(frame: frame)
self.commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.commonInit()
}
func commonInit(){
let bundle = Bundle(for: Self.self)
let nib = UINib(nibName: String(describing: type(of: self)), bundle: bundle)
nib.instantiate(withOwner: self, options: nil)
addSubview(contentView)
contentView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
contentView.topAnchor.constraint(equalTo: topAnchor),
contentView.bottomAnchor.constraint(equalTo: bottomAnchor),
contentView.leadingAnchor.constraint(equalTo: leadingAnchor),
contentView.trailingAnchor.constraint(equalTo: trailingAnchor)
])
}