15ongm 15ongm - 2 months ago 10
Swift Question

Custom UITableViewCell not showing textfield

I created a custom UITableViewCell with a label on the left and a text field on the right. However, on the table, the label shows up, but the textfield does not. Why is that happening and how can I fix this?

class TableViewCell: UITableViewCell {
var cellLabel: UILabel!
var textField: UITextField!

let tableViewController:TableViewController = TableViewController()

init(frame: CGRect, title: String) {
super.init(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")

cellLabel = UILabel(frame: CGRectMake(0, 0, 100, 40))

let cellTextField = UITextField(frame: CGRectMake(bounds.width*0.2, 100, bounds.width*0.75, 50))
cellTextField.delegate = tableViewController

self.addSubview(cellLabel)
self.addSubview(cellTextField)
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}

}

class TableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate {
var tableView: UITableView = UITableView()
let cellLabels : [String] = ["ID","Number of Times Infected"]

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
var cell = TableViewCell(frame: CGRectMake(0, 0, self.view.frame.width, 50), title: cellLabels[indexPath.row])
cell.cellLabel!.text = cellLabels [indexPath.row]
return cell;
}
}

Answer
//TableViewCell Class
class TableViewCell: UITableViewCell {

    var cellLabel: UILabel!
    var textField: UITextField!

    let tableViewController:ViewController = ViewController()

    init(frame: CGRect, title: String) {
        super.init(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")

        cellLabel = UILabel(frame: CGRectMake(0, 0, bounds.width/2, 40))

        let cellTextField = UITextField(frame: CGRectMake(cellLabel.frame.size.width+5, 0, bounds.width*0.75, 50))

        cellTextField.placeholder = "Enter Your Text here"
        cellTextField.delegate = tableViewController

        self.addSubview(cellLabel)
        self.addSubview(cellTextField)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }
//View Controller Class
class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate
{
    var tableView: UITableView  =   UITableView()
    let cellLabels : [String] = ["ID","Number of Times Infected"]

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return cellLabels.count
    }
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
        let cell = TableViewCell(frame: CGRectMake(0, 0, self.view.frame.width, 50), title: cellLabels[indexPath.row])
        cell.cellLabel!.text = cellLabels [indexPath.row]
        return cell;
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }


}

Note: Set Frame as per your Requirements.

Output:

ScreenShot