Nate23VT
Nate23VT

Reputation: 423

cellForRowAt indexPath function not called - Swift tableView

I have a tableView that is displayed in my Storyboard and in the Preview but when I run it on my device it does not display. When debugging, I realize that the issue seems to be that it never runs the cellForRowAt indexPath function. Why does this code not execute that function, I ould expect for it to output 2 rows.

EDIT: numberOfRowsInSection is being called and returns 2 for the data.count

Here is my ViewController code:

import UIKit

class WRViewController: PopupViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var wrNo: UILabel!
@IBOutlet weak var tableView: UITableView!

struct Contact {

    var name:String
    var type:String
    var phone:String

}
var data:[Contact]=[Contact]()

override func viewDidLoad() {

    super.viewDidLoad()

    data.append(Contact(name:"John Doe",type:"Builder",phone:"1234567890"))
    data.append(Contact(name:"Jane Doe",type:"Contractor",phone:"0987654321"))

    tableView.delegate = self
    tableView.dataSource = self

    tableView.reloadData()

}

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

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "contactCell", for: indexPath)

    let contactView = data[indexPath.row]
    cell.text = contactView.name
    cell.image = contactView.type
    cell.text = contactView.phone

    return cell
}

}

Upvotes: 1

Views: 7169

Answers (1)

matt
matt

Reputation: 535169

The problem is that you have not supplied sufficient autolayout constraints to determine the height of this table view. Therefore it has no height. Therefore it doesn't need to show any cells. Therefore it doesn't ask for any cells.

The storyboard and/or the view debugger has been warning you quite clearly that this is a problem, but you didn't pay any attention. Well, pay attention! Add a height constraint or top and bottom constraints, so that this table view has a height.

(Note that the problem might not be just with the table view, but with whatever it is vertically pinned to. But you have given no information about that, so it's impossible to say, from here.)

Upvotes: 10

Related Questions