【发布时间】:2017-07-28 07:43:30
【问题描述】:
我正在寻找在 Swift 中使用可重用单元的正确方法。
如何使setup 方法只被调用一次?
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "AwesomeCell")
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCell(withIdentifier: "AwesomeCell") as UITableViewCell? ?? UITableViewCell()
self.setup(cell: cell) // I want this to be called only once
cell.textLabel?.text = self.data[indexPath.row]
return cell
}
func setup(cell: UITableViewCell) {
cell.accessoryType = .disclosureIndicator
}
谢谢。
【问题讨论】:
-
只是好奇,为什么你需要在一个单独的函数中做呢?如果您不总是想设置 cell.accessoryType = .disclosureIndicator ,为什么不在设置之前检查单元格的accessoryType 是否已经是一个disclosureIndicator?如果它变得复杂,为什么不将 UITableViewCell 子类化并对其进行适当的设置?
-
@Joshua 这只是一个例子。
-
那么我能看到的最好的方法是子类化并在init上进行设置
标签: ios swift uitableview swift3