我想为iPad和iPhone加载不同的xib文件。为此,我创建了两个不同的xib文件,例如sample〜ipad.xib,sample〜iphone.xib,并将文件注册到我的视图控制器中,如下所示,
static let k_SampleCellIdentifier = "SampleTableCellIdentifier"
func myTableViewCells() {
let sampleTableCell = UINib.init(nibName: "SampleDataTableViewCell",
bundle: nil)
self.tableView.register(sampleTableCell,
forCellReuseIdentifier: SampleProcessViewController.k_SampleCellIdentifier)
}
此外,从sample〜iPad.xib文件中,我已将所有属性IB插座连接到单元标识符类。现在,我的编码更改仅反映在iPad上,而不反映在iPhone设备上。我尝试了许多解决方案,但没有任何效果。 TIA。
您可以通过使用布尔值来实现。
var isIpad = UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.pad
static let k_IPhoneCellIdentifier = "IPhoneCellIdentifier"
static let k_IPadCellIdentifier = "IPadCellIdentifier"
func myTableViewCells() {
let iPadTableCell = UINib.init(nibName: "IPadCellIdentifier",
bundle: nil)
let iPhoneTableCell = UINib.init(nibName: "IPhoneCellIdentifier",
bundle: nil)
self.tableView.register(isIpad ? iPadTableCell : iPhoneTableCell,
forCellReuseIdentifier: isIpad ? k_IPadCellIdentifier : k_IPhoneCellIdentifier)
}