永远不会调用cellForRowAtIndexPath

问题描述 投票:4回答:3

我正在使用Xcode 6 beta 7.我有一个故事板,其中包含UIViewController,它有UITableview和UITableViewCell,所有内容都正确连接,我已经为UIViewController提供了所有必需的代码。

当我在模拟器中运行应用程序时,从不调用其中一个数据源函数“cellForRowAtIndexPath”,但调用“numberOfRowsInSection”

  • “func tableView(tableView:UITableView,numberOfRowsInSection section:Int) - > Int”被调用
  • 但是“func tableView(tableView:UITableView,cellForRowAtIndexPath indexPath:NSIndexPath) - > UITableViewCell”从未被调用过

下面是我的视图控制器代码:

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;
    }
}

有人可以建议为什么从不调用“cellForRowAtIndexPath”,它曾经在Xcode6 beta 2中工作。

swift ios8 xcode6-beta7
3个回答
3
投票

这是我做的,卸载Xcode 5和6只重新安装的Xcode 6,Ran项目和它工作。经过2天的调试后,重新安装完成了这个技巧。


2
投票

我刚刚检查了你的代码,它确实有效。 检查numberOfRowsInSection是否被调用。如果没有检查storyboard的连接。


0
投票

试试这个:

class MainViewController:UIViewController,UITableViewDataSource,UITableViewDelegate {

func viewDidLoad(){

super.viewDidLoad()

tableView.dataSource = self

tableView.delegate = self

}

}

© www.soinside.com 2019 - 2024. All rights reserved.