ios 带有 XIB Swift 的 UITableViewCell 子类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28489720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UITableViewCell Subclass with XIB Swift
提问by jamespick
I have a UITableViewCell
subclass NameInput
that connects to an xib with a custom init
method.
我有一个使用自定义方法连接到 xib的UITableViewCell
子类。NameInput
init
class NameInput: UITableViewCell {
class func make(label: String, placeholder: String) -> NameInput {
let input = NSBundle.mainBundle().loadNibNamed("NameInput", owner: nil, options: nil)[0] as NameInput
input.label.text = label
input.valueField.placeholder = placeholder
input.valueField.autocapitalizationType = .Words
return input
}
}
Is there a way I can initialize this cell in the viewDidLoad
method and still reuse it? Or do I have to register the class itself with a reuse identifier?
有没有办法可以在viewDidLoad
方法中初始化这个单元格并仍然重用它?或者我是否必须使用重用标识符注册类本身?
回答by Rob
The customary NIB process is:
习惯的NIB流程是:
Register your NIB with the reuse identifier. In Swift 3:
override func viewDidLoad() { super.viewDidLoad() tableView.register(UINib(nibName: "NameInput", bundle: nil), forCellReuseIdentifier: "Cell") }
In Swift 2:
override func viewDidLoad() { super.viewDidLoad() tableView.registerNib(UINib(nibName: "NameInput", bundle: nil), forCellReuseIdentifier: "Cell") }
Define your custom cell class:
import UIKit class NameInput: UITableViewCell { @IBOutlet weak var firstNameLabel: UILabel! @IBOutlet weak var lastNameLabel: UILabel! }
Create a NIB file in Interface Builder (with the same name referenced in step 1):
Specify the base class of the tableview cell in the NIB to reference your custom cell class (defined in step 2).
Hook up references between the controls in the cell in the NIB to the
@IBOutlet
references in the custom cell class.
Your
cellForRowAtIndexPath
would then instantiate the cell and set the labels. In Swift 3:override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! NameInput let person = people[indexPath.row] cell.firstNameLabel.text = person.firstName cell.lastNameLabel.text = person.lastName return cell }
In Swift 2:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! NameInput let person = people[indexPath.row] cell.firstNameLabel.text = person.firstName cell.lastNameLabel.text = person.lastName return cell }
使用重用标识符注册您的 NIB。在 Swift 3 中:
override func viewDidLoad() { super.viewDidLoad() tableView.register(UINib(nibName: "NameInput", bundle: nil), forCellReuseIdentifier: "Cell") }
在 Swift 2 中:
override func viewDidLoad() { super.viewDidLoad() tableView.registerNib(UINib(nibName: "NameInput", bundle: nil), forCellReuseIdentifier: "Cell") }
定义您的自定义单元格类:
import UIKit class NameInput: UITableViewCell { @IBOutlet weak var firstNameLabel: UILabel! @IBOutlet weak var lastNameLabel: UILabel! }
在 Interface Builder 中创建一个 NIB 文件(与步骤 1 中引用的名称相同):
在 NIB 中指定 tableview 单元格的基类以引用您的自定义单元格类(在步骤 2 中定义)。
将 NIB 中单元格中控件之间的
@IBOutlet
引用与自定义单元格类中的引用连接起来。
cellForRowAtIndexPath
然后您将实例化单元格并设置标签。在 Swift 3 中:override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! NameInput let person = people[indexPath.row] cell.firstNameLabel.text = person.firstName cell.lastNameLabel.text = person.lastName return cell }
在 Swift 2 中:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! NameInput let person = people[indexPath.row] cell.firstNameLabel.text = person.firstName cell.lastNameLabel.text = person.lastName return cell }
I wasn't entirely sure from your example what controls you placed on your cell, but the above has two UILabel
controls. Hook up whatever @IBOutlet
references make sense for your app.
从您的示例中,我并不完全确定您在单元格上放置了哪些控件,但上面有两个UILabel
控件。连接任何@IBOutlet
对您的应用程序有意义的引用。
回答by rob mayoff
You don't initialize cells in viewDidLoad
. You should register the XIB, not the class, with your table view. You should set up the label and text field in tableView:cellForRowAtIndexPath:
(possibly by calling an instance method on NameInput
).
您不会在viewDidLoad
. 您应该使用表视图注册 XIB,而不是类。您应该在中设置标签和文本字段tableView:cellForRowAtIndexPath:
(可能通过在 上调用实例方法NameInput
)。