简体   繁体   中英

How to create cumstom TableViewCells dynamically in Swift?

I want to create custom TableViewCells and therefore I created some classes typeACell/typeBCell that extend from UITableViewCell.

Now in

tableView-cellForRowAtIndexPath -> UITableViewCell

I want to return the cells dynamically. How is it possible to return my custom cells when I want to create the return value only once via

let dynCell:UITableViewCell!

What do I have to change in my approach

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
}

This throws the error:

Variable 'dynCell' used before being initialized

Thnx!

Try this(using your code):

    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

You get this error because the condition for initializing cell is unclear. First if statement has an else branch, but the second if statement does not. If both conditions are false, the cell will remain uninitialized.

You can fix this by adding a second else branch:

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

Or you can remove second if statement if you are sure that cell will be TypeB:

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

when I change my let to var and unpack the values it seems to work - but is this the correct way?

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM