带有 XIB Swift 的 UITableViewCell 子类 [英] UITableViewCell Subclass with XIB Swift
问题描述
我有一个 UITableViewCell
子类 NameInput
,它使用自定义的 init
方法连接到一个 xib.
I have a UITableViewCell
subclass NameInput
that connects to an xib with a custom init
method.
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
}
}
有什么方法可以在 viewDidLoad
方法中初始化这个单元格并且仍然重用它?还是我必须使用重用标识符注册类本身?
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?
推荐答案
习惯的NIB流程是:
使用重用标识符注册您的 NIB.在 Swift 3 中:
Register your NIB with the reuse identifier. In Swift 3:
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UINib(nibName: "NameInput", bundle: nil), forCellReuseIdentifier: "Cell")
}
在 Swift 2 中:
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!
}
在 Interface Builder 中创建一个 NIB 文件(与步骤 1 中引用的名称相同):
Create a NIB file in Interface Builder (with the same name referenced in step 1):
在 NIB 中指定 tableview 单元格的基类以引用您的自定义单元格类(在步骤 2 中定义).
Specify the base class of the tableview cell in the NIB to reference your custom cell class (defined in step 2).
将 NIB 中单元格中控件之间的引用与自定义单元格类中的 @IBOutlet
引用关联起来.
Hook up references between the controls in the cell in the NIB to the @IBOutlet
references in the custom cell class.
您的 cellForRowAtIndexPath
然后将实例化单元格并设置标签.在 Swift 3 中:
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
}
在 Swift 2 中:
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
}
从您的示例中我不能完全确定您在单元格上放置了哪些控件,但上面有两个 UILabel
控件.连接任何对您的应用有意义的 @IBOutlet
引用.
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.
这篇关于带有 XIB Swift 的 UITableViewCell 子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!