tableView cellForRowAtIndexPath方法未被调用

时间:2015-05-29 17:16:53

标签: ios uitableview swift

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath:   NSIndexPath) -> UITableViewCell {
    print("WTF is this getting hit...")

    let Cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! UITableViewCell

    Cell.textLabel?.text = self.funlists[indexPath.row]

    print("This better be getting hit")


    return Cell;
}

由于某种原因,这个方法没有被调用。

我已设置以下

    uiTableView.delegate=self
    uiTableView.dataSource=self
    uiTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")

我也包括了,

class viewUniversityList: UIViewController, UITableViewDelegate, UITableViewDataSource {

3 个答案:

答案 0 :(得分:2)

这可能有几个原因。其中一些是:

  • 如果您在ViewController文件中使用tableView,那么您应该添加UITableViewDelegateUITableViewDelegate个委托,如下所示:

    class ViewController: UIViewController,UITableViewDelegate, UITableViewDelegate { ... }
    
  • 如果您在代码中创建了tableView,那么您应该执行此操作 以下:

    tableView.delegate = self
    tableView.dataSource = self
    tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "reuseIdentifier")
    
  • 如果这一切都已完成,或者您已通过故事板创建UITableView并使用单独的类UITableView的子类,那么肯定需要定义该 以下方法:

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {...}
    
@rmaddy建议

func numberOfSectionsInTableView(tableView: UITableView) -> Int {...}是可选的,但定义它是一个很好的做法。

答案 1 :(得分:1)

{p {1}}上未设置

delegate属性? 还应实施tableView

答案 2 :(得分:1)

您必须设置代理并声明您使用这些协议

在didLoad中的file.m中

_table.delegate = self;
_table.dataSource = self;
当你声明界面时,在file.h中

你必须添加这些协议UITableViewDelegateUITableViewDataSource

Xcode将告诉您必须实施哪些方法来尊重协议。