带有 XIB 的 UITableViewCell 子类 Swift
UITableViewCell Subclass with XIB Swift
我有一个 UITableViewCell
subclass NameInput
,它使用自定义 init
方法连接到 xib。
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
方法中初始化此单元格并仍然重用它?或者我是否必须使用重用标识符注册 class 本身?
您没有在 viewDidLoad
中初始化单元格。您应该使用 table 视图注册 XIB,而不是 class。您应该在 tableView:cellForRowAtIndexPath:
中设置标签和文本字段(可能通过在 NameInput
上调用实例方法)。
习惯的NIB流程是:
使用重用标识符注册您的 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")
}
定义自定义单元格class:
import UIKit
class NameInput: UITableViewCell {
@IBOutlet weak var firstNameLabel: UILabel!
@IBOutlet weak var lastNameLabel: UILabel!
}
在 Interface Builder 中创建一个 NIB 文件(与步骤 1 中引用的名称相同):
在 NIB 中指定表视图单元格的基数 class 以引用您的自定义单元格 class(在步骤 2 中定义)。
将 NIB 单元格中的控件之间的引用连接到自定义单元格 class 中的 @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
}
根据您的示例,我不能完全确定您在单元格上放置了什么控件,但上面有两个 UILabel
控件。连接对您的应用有意义的任何 @IBOutlet
参考。
我有一个 UITableViewCell
subclass NameInput
,它使用自定义 init
方法连接到 xib。
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
方法中初始化此单元格并仍然重用它?或者我是否必须使用重用标识符注册 class 本身?
您没有在 viewDidLoad
中初始化单元格。您应该使用 table 视图注册 XIB,而不是 class。您应该在 tableView:cellForRowAtIndexPath:
中设置标签和文本字段(可能通过在 NameInput
上调用实例方法)。
习惯的NIB流程是:
使用重用标识符注册您的 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") }
定义自定义单元格class:
import UIKit class NameInput: UITableViewCell { @IBOutlet weak var firstNameLabel: UILabel! @IBOutlet weak var lastNameLabel: UILabel! }
在 Interface Builder 中创建一个 NIB 文件(与步骤 1 中引用的名称相同):
在 NIB 中指定表视图单元格的基数 class 以引用您的自定义单元格 class(在步骤 2 中定义)。
将 NIB 单元格中的控件之间的引用连接到自定义单元格 class 中的
@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 }
根据您的示例,我不能完全确定您在单元格上放置了什么控件,但上面有两个 UILabel
控件。连接对您的应用有意义的任何 @IBOutlet
参考。