1

I've designed a tableView and tableViewCell programatically, without the use of storyboards. My viewDidLoad() in the ViewController looks something like this:

tableView.delegate = self
tableView.dataSource = self
tableView.register(TicketsTableViewCell.self,forCellReuseIdentifier:cellReuseIdentifier)
tableView = UITableView(frame: UIScreen.main.bounds, style: .plain)
self.view.addSubview(tableView)

And my tableViewCell looks something like this:

class TicketsTableViewCell: UITableViewCell {

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

    //Other View related stuff
}

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

override func layoutSubviews() {
    super.layoutSubviews()
}

The thing is, when I run it, I'm able to see the tableView, but not the cells. Furthermore, when I add a breakpoint at cellForRowAt:, it does not get called. What is that I'm doing wrong? Is there something wrong I'm doing with the reuse identifier? Thanks in advance.

shravan.sukumar
  • 109
  • 2
  • 11

2 Answers2

2

The problem is first you have set delegate and datasource of tableView after that you are re-initializing the tableView with line tableView = UITableView(frame: UIScreen.main.bounds, style: .plain) so put that line first then set delegate and datasource and register cell.

tableView = UITableView(frame: UIScreen.main.bounds, style: .plain)
tableView.delegate = self
tableView.dataSource = self
tableView.register(TicketsTableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier)
self.view.addSubview(tableView)
Nirav D
  • 71,513
  • 12
  • 161
  • 183
0

Try like this may help for you

 class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource
 {

var tableView : UITableView!


override func viewDidLoad() {
    super.viewDidLoad()

    self.view.backgroundColor = UIColor.whiteColor()
    self.setUpTableView()

}
func setUpTableView()
{
    // Create only one table view.
    tableView = UITableView(frame: CGRectMake(self.view.frame.size.width / 10, self.view.frame.size.height / 2, self.view.frame.size.width - self.view.frame.size.width / 5, self.view.frame.size.height / 2 - 20), style: UITableViewStyle.Grouped)
    tableView.dataSource = self
    tableView.delegate = self
    tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
    tableView.layer.cornerRadius = 10
    tableView.layer.borderColor = UIColor.blackColor().CGColor
    tableView.layer.borderWidth = 2
    self.view.addSubview(tableView)
}
//table view data source

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return 3
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell : UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell

    cell.textLabel?.text = "test"
    cell.textLabel?.numberOfLines = 0

    return cell

  }

 }
Mitesh jadav
  • 910
  • 1
  • 9
  • 26