rob*_*obz 2 iphone uitableview ios swift
我有一个自定义的 UITableViewCell,我用界面生成器设计它有一个标签和一个选择器。对于实现 swift 文件,我添加了两个出口来引用标签和选择器。插座连接到 ui 元素,代码如下所示
class TitlePickerCell: UITableViewCell
{
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var picker: UIPickerView!
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
}
}
Run Code Online (Sandbox Code Playgroud)
我有一个 UITableViewController 我想使用这个单元格。我将其设置为静态,因为我使用它来显示设置,并且单元格数量永远不会以编程方式更改。我将自定义单元格之一添加到表视图中,并将其标识符设置为 MediaStyleCell。
然后,在 UITableViewController 的 swift 文件中,我将这一行添加到我的 viewDidLoad 函数中
self.settingsTable.registerNib(UINib.init(nibName: "TitlePickerCell", bundle: nil), forCellReuseIdentifier: "MediaStyleCell")
Run Code Online (Sandbox Code Playgroud)
其中 TitlePickerCell 是 .xib 文件的名称,MediaStyleCell 是我使用界面生成器为单元格提供的标识符。当我运行我的应用程序时,它崩溃了,因为我的自定义单元类中的标签和选择器为零。
如果您使用 tableview.registerClass:forCellReuseIdentifier:但您也有单元格的 xib 文件,您将收到此错误。
所以而不是
tablView.register(Cell.self, forCellReuseIdentifier: cellToDisplay.identifier)
Run Code Online (Sandbox Code Playgroud)
只需使用
tableView.register(UINib(nibName: cellToDisplay.identifier, bundle: nil), forCellReuseIdentifier: cellToDisplay.identifier)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7062 次 |
| 最近记录: |