简体   繁体   中英

cellForRowAtIndexPath not called after reloadData

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!

Try setting the delegate and dataSource of your UITableView :

myTable.delegate = self
myTable.dataSource = self

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM