gpt4 book ai didi

swift - 如何在 Swift 中动态创建自定义表格 View 单元格?

转载 作者:行者123 更新时间:2023-11-28 13:06:29 26 4
gpt4 key购买 nike

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

现在在

tableView-cellForRowAtIndexPath -> UITableViewCell

我想动态返回单元格。当我只想通过

创建一次返回值时,如何返回我的自定义单元格
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

关于swift - 如何在 Swift 中动态创建自定义表格 View 单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32603945/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com