headkit
headkit

Reputation: 3327

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!

Upvotes: 0

Views: 62

Answers (3)

headkit
headkit

Reputation: 3327

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
}

Upvotes: 0

ifau
ifau

Reputation: 2120

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

Upvotes: 1

ciccioska
ciccioska

Reputation: 1291

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

Upvotes: 1

Related Questions