0
votes

I have a table view controller that needs to be updated through a delegate call. I have set the datasource and delegate and on initial load of the tableview, all works as expected. I have a delegate method that gets called after a datasource update. The delegate calls a refresh method in the table view controller class which calls .reloadData()

When reloadData is called, numberOfRowsInSection is called and accurately returns the number of rows, however cellForRowAtIndexPath never gets called.

In this particular case, numberOfRowsInSection returns 2, therefore cellForRowAtIndexPath should be called twice but it's called zero times.

On initial load everything is fine. It's only when reloadData is called taht cellForRowAtIndexPath is ignored. I have done this same thing many times in Obj-C without any weirdness. Are there any known issues with this in Swift?

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

    let cell = tableView.dequeueReusableCellWithIdentifier(LayerMenuCell.reuseId) as! LayerMenuCell
    // ....
    return cell
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    print(layerEntries?.count)
    return (layerEntries?.count)!
}

func refresh() {
    self.layersTableView.reloadData()
}

Thanks!

3
Can you share the code?Syed Qamar Abbas
See post for code. Code never enters cellForRow... when reloadData is called, even though numberOfRowsInSections returns an accurate number that is NOT zeroPheepster
Check if you're using 'numberOfSectionsInTableView' and make sure its not returning ZERO.Aziz
if reload table doesnt work, try calling relaodROws / reload sections and see if cellFor row is called or not. If number or rows is called, then cell for row shud get called too.Teja Nandamuri
Do you perform the reloadData call on the main thread?Michi

3 Answers

4
votes

Try setting the delegate and dataSource of your UITableView:

myTable.delegate = self
myTable.dataSource = self
0
votes

As specified in the Documentation:

A functioning table view requires three table view data source methods.

func numberOfSectionsInTableView(tableView: UITableView) -> Int
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell

MAake sure you implement the above three delegate methods and they return some values other than nil or 0.

There is a chance that cell height could be 0/ table height is 0 in both the cases cell for row method will not get called.

Also make sure you set the delegate properly and call the reloadData method on main thread. More on here

-2
votes

Things you need to check when Tableview is not working as expected:
1. Setting the delegate and datasource through storyboard or by code.

self.tableView.delegate = self
self.tableView.dataSource = self

2.check if tableView in storyboard is connected to tableView outlet.
3.check numberOfRowsInSection and numberOfSectionsInT‌​ableView returning the correct values.
4.check if the methods is written properly.
5.add the delegate and datasource after UIViewController.

<UITableViewDelegate , UITableViewDataSource>

this will help you if you are missing any thing.