简体   繁体   中英

cellForRowAtIndexPath is never called

I have a storyboard which contains UIViewController which has UITableview and UITableViewCell , everything is hooked up properly and I have put all the required code for UIViewController.

When I run the app in the simulator one of the datasource function cellForRowAtIndexPath is never called but numberOfRowsInSection is called

  • "func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int" is called
  • But "func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell" IS NEVER CALLED

Below is my code for view controller:

import UIKit

class DVViewController: UIViewController, UITableViewDelegate, UITableViewDataSource
{

    @IBOutlet var tableView : UITableView?
    var tableData = ["Row 1", "Row 2", "Row 3"]
    let kCellID = "cell"

    override func viewDidLoad()
    {
        super.viewDidLoad()

        self.tableView?.delegate = self;
        self.tableView?.dataSource = self;

        //load cell
        self.tableView?.registerClass(UITableViewCell.self, forCellReuseIdentifier: kCellID);
        self.tableView?.reloadData();
    }

    //MARK: Tableview delegates
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return self.tableData.count;
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
       //var cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: kCellID)
        let cell : UITableViewCell = UITableViewCell (style: UITableViewCellStyle.Default, reuseIdentifier: kCellID);
        return cell;
    }
}

Can someone suggest why cellForRowAtIndexPath is never called?

Here is what I did, uninstalled Xcode 5 & 6 both the reinstalled Xcode 6 only, Ran the project and it WORKED. After 2 days of debugging its the reinstall which did the trick.

I have just checked your code, it works.
Check to see whether or not numberOfRowsInSection is called. if not check the connection in storyboard .

Try this:

class MainViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{

func viewDidLoad(){

super.viewDidLoad()

tableView.dataSource = self

tableView.delegate = self

}

}

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