1

I have a two custom cells implemented:

class TextFieldCell: UITableViewCell{
    var label = UILabel()
    var textField = UITextField()

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        label.text = "Name"
        textField.placeholder = "Enter Task Name Here"
        addSubview(label)
        addSubview(textField)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
}

class SwitchCell : UITableViewCell {
    var label = UILabel()
    var switchControl = UISwitch()

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        label.text = "State"
        switchControl.on = true
        addSubview(label)
        addSubview(switchControl)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
}

In my table view class i have declared this two cells:

let textFieldCell = TextFieldCell(style: .Default, reuseIdentifier: "textFieldCell")
let switchCell = SwitchCell(style: .Default, reuseIdentifier: "switchCell")

and have delegate method in it:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        switch indexPath.row {
        case 0: return textFieldCell
        case 1: return switchCell
        default: return UITableViewCell()
        }
    }

But when table view loads i've see only switch control on the left of second row. I don't know why.

5
  • have you set all datasource and delegate? Commented Feb 18, 2016 at 11:53
  • there should be more than one row in numberOfRowsInSection block and try to set label text colour black (it might be same as background colour, so use different colours). Commented Feb 18, 2016 at 12:01
  • @Madangupta yes it is (return 2 in numberOfRowsInSection) different colours dont helps. Commented Feb 18, 2016 at 12:11
  • @AndreyM. I have added the solutions. Please go through it . It will definitely solve your problem Commented Feb 18, 2016 at 12:31
  • if let cell = tableView.dequeueReusableCellWithIdentifier("switchCell") as? SwitchCell { - You should normally have something like this, in your cellForRowAtIndexPath Commented Feb 18, 2016 at 13:21

2 Answers 2

1

Try this:

private let reuseIdentifier = "switchCell"
//identifier of your cell.

Seems you have missed registering the cell.

    let tableNib = UINib(nibName:"SwitchCell", bundle:nil)
    self.tableView!.registerNib(tableNib,forCellReuseIdentifier:reuseIdentifier)
Sign up to request clarification or add additional context in comments.

Comments

0

TextFieldCell Added below Code in ViewDidLoad of TableViewCell


self.tableView.registerClass(SwitchCell, forCellReuseIdentifier: "switchCell")
self.tableView.registerClass(TextFieldCell, forCellReuseIdentifier: "textFieldCell")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.