我在详细视图控制器中构建UITableView
。该表视图的某些方面包括:
PhotoCell
和InfoCell
,两者都扩展为UITableViewCell
。UITableViewCell
的每个子类中,我创建了一个static createCell()
方法,该方法使用tableView.dequeueReusableCellWithIdentifier
返回已初始化的已填充单元格。dequeueReusableCellWithIdentifier
是否可接受(甚至有用)?我采用的方法是定义包含单元格数组的source
属性。我不确定这是否是处理它的最佳方式。
PlaceViewController + UITableViewDataSource.swift
extension PlaceViewController: UITableViewDataSource {
var source: [UITableViewCell] {
get {
var cells = [UITableViewCell]()
if let cell = SummaryCell.createCell(tableView, text: place?.generalInfo) {
cells.append(cell)
}
// ^^ repeat above for each cell in table
return cells
}
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return source.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
return source[indexPath.row]
}
}
SummaryCell.swift
class SummaryCell : UITableViewCell {
@IBOutlet var summaryLabel: UILabel!
static func createCell(tableView: UITableView, text: String?) -> SummaryCell? {
if let text = text {
let cell = tableView.dequeueReusableCellWithIdentifier("SummaryCell") as! SummaryCell
cell.summaryLabel.text = text
return cell
}
return nil
}
}
答案 0 :(得分:0)
如果您正在为表使用不同的单元格,也可以进行条件检查或在特定索引中切换所需的单元格类型,然后尝试使用已创建的单元格进行双击 - 如果不初始化新的一个。
另外,看看你的代码,每当调用cellForRowAtIndexPath时,你就会创建一个新的单元格数组,这是不好的。