user3766930 user3766930 - 2 months ago 7
Swift Question

why I don't see any cells in my UITableView in Swift?

I have a

UIViewController
and I put on it
TableView
:

enter image description here

I also added the
UITableViewCell
to it, checked in story board that my table view has dynamic-generated content and added an identifier "cell" to the
UITableViewCell
. I connected it with outlets to my class and I set up the inheritance from
UITableViewDelegate
:

class MapViewController: UIViewController, UITableViewDelegate, UITextViewDelegate {

@IBOutlet weak var myTableDetailsPanel: UITableView!

override func viewDidLoad() {
super.viewDidLoad()
myTableDetailsPanel.delegate = self
}


Then I add my custom object to my array:

var items = NSMutableArray()
...
print("size of items before: \(items.count)")
items.addObject(myObject)
print("size of items after: \(items.count)")
myTableDetailsPanel.reloadData()
myTableDetailsPanel.hidden = false


I also have this method:

func tableView(myTableDetailsPanel: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = myTableDetailsPanel.dequeueReusableCellWithIdentifier("cell") as! MyDetailsCell
let yawp:MySingleObject = self.items[indexPath.row] as! MySingleObject
print("hello")
...


And... my problem is that the panel appears empty, in the console I see:

size of items before: 0
size of items after: 1


but somehow I don't see
hello
. My code from
cellForRowAtIndexPath
never runs. Why so?

Answer

As you implement the UITableViewDelegate protocol to be notified about changes in the UITableView you have to implement the UITableViewDataSource, the data source provides to table-view object with the information it needs to construct and modify a table view.

So you need to implement UITableViewDataSource protocol and declare the dataSource as self like this:

myTableDetailsPanel.dataSource = self

I hope this help you.