如何在 Swift 中动态创建自定义表格视图单元格?

How to create cumstom TableViewCells dynamically in Swift?

我想创建自定义 TableViewCell,因此我创建了一些 类 typeACell/typeBCell 从 UITableViewCell 扩展而来。

现在

tableView-cellForRowAtIndexPath -> UITableViewCell

我想 return 动态地处理单元格。当我只想通过

创建一次 return 值时,如何 return 我的自定义单元格
let dynCell:UITableViewCell!

我的方法需要改变什么

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{

    let cellTypeSelector = DynamicCellSelector.getTypeAnyhow()
    let dynCell:UITableViewCell!

    if cellTypeSelector.isTypeA() {
        dynCell = tableView.dequeueReusableCellWithIdentifier("typeACell", forIndexPath: indexPath) as? TypeACell
        (dynCell as! TypeACell).titleBar.text = ""
    }
    else if cellTypeSelector.isTypeB() {
        dynCell = tableView.dequeueReusableCellWithIdentifier("typeBCell", forIndexPath: indexPath) as? TypeBCell
        (dynCell as! TypeBCell).titleBar.text = ""
    }
    else {
        dynCell = tableView.dequeueReusableCellWithIdentifier("unknownTypeCell", forIndexPath: indexPath) as? UnknownTypeCell
        (dynCell as! UnknownTypeCell).titleBar.text = ""
    }

    return dynCell
}

这会引发错误:

Variable 'dynCell' used before being initialized

谢谢!

试试这个(使用您的代码):

    let cellTypeSelector = DynamicCellSelector.getTypeAnyhow()
    let dynCell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("typeACell", forIndexPath: indexPath) as? TypeACell;
    if cellTypeSelector.isTypeB() {
        dynCell = tableView.dequeueReusableCellWithIdentifier("typeBCell", forIndexPath: indexPath) as? TypeBCell
        (dynCell as! TypeBCell).titleBar.text = ""
    }else{
        (dynCell as! TypeACell).titleBar.text = ""
    }

    return dynCell

您收到此错误是因为初始化单元格的条件不明确。第一个 if 语句有一个 else 分支,但第二个 if 语句没有。如果两个条件都为假,单元将保持未初始化状态。

您可以通过添加第二个 else 分支来解决此问题:

if cellTypeSelector.isTypeA() {
    dynCell = tableView.dequeueReusableCellWithIdentifier("typeACell", forIndexPath: indexPath) as? TypeACell
    (dynCell as! TypeACell).titleBar.text = ""
}
else if cellTypeSelector.isTypeB() {
    dynCell = tableView.dequeueReusableCellWithIdentifier("typeBCell", forIndexPath: indexPath) as? TypeBCell
    (dynCell as! TypeBCell).titleBar.text = ""
}
else {
    dynCell = UITableViewCell()
}

return dynCell

或者如果您确定单元格将是 TypeB,您可以删除第二个 if 语句:

if cellTypeSelector.isTypeA() {
    dynCell = tableView.dequeueReusableCellWithIdentifier("typeACell", forIndexPath: indexPath) as? TypeACell
    (dynCell as! TypeACell).titleBar.text = ""
}
else {
    dynCell = tableView.dequeueReusableCellWithIdentifier("typeBCell", forIndexPath: indexPath) as? TypeBCell
    (dynCell as! TypeBCell).titleBar.text = ""
}

return dynCell

当我将我的 let 更改为 var 并解压缩它似乎有效的值时 - 但这是正确的方法吗?

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{

    let cellTypeSelector = DynamicCellSelector.getTypeAnyhow()
    var dynCell:UITableViewCell!

    if cellTypeSelector.isTypeA() {
        dynCell = (tableView.dequeueReusableCellWithIdentifier("typeACell", forIndexPath: indexPath) as? TypeACell)!
        (dynCell as! TypeACell).titleBar.text = ""
    }
    else if cellTypeSelector.isTypeB() {
        dynCell = (tableView.dequeueReusableCellWithIdentifier("typeBCell", forIndexPath: indexPath) as? TypeBCell)!
        (dynCell as! TypeBCell).titleBar.text = ""
    }
    else {
        dynCell = (tableView.dequeueReusableCellWithIdentifier("unknownTypeCell", forIndexPath: indexPath) as? UnknownTypeCell)!
        (dynCell as! UnknownTypeCell).titleBar.text = ""
    }

    return dynCell
}