简体   繁体   English

tableView cellForRowAtIndexPath方法没有被调用

[英]tableView cellForRowAtIndexPath method not getting called

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

for some reason this method isn't getting called. 由于某种原因未调用此方法。

i have set the following 我设置了以下内容

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

and I have also included, 而且我还包括了

class viewUniversityList: UIViewController, UITableViewDelegate, UITableViewDataSource {

There might be several reason for this. 可能有几个原因。 Some of them are : 他们之中有一些是 :

  • If you are using tableView in your ViewController file, then you should add the UITableViewDelegate and UITableViewDelegate delegates, like this : 如果在ViewController文件中使用tableView,则应添加UITableViewDelegateUITableViewDelegate委托,如下所示:

     class ViewController: UIViewController,UITableViewDelegate, UITableViewDelegate { ... } 
  • If you have created a tableView in code then you should do the following : 如果已在代码中创建了tableView ,则应执行以下操作:

     tableView.delegate = self tableView.dataSource = self tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "reuseIdentifier") 
  • If that's all done, or you have create UITableView through storyboard with a separate class which is a subClass of UITableView , then you definitely need to define the following methods : 如果这一切都完成后,或你创建UITableView通过故事板有独立的类,这是一个子类UITableView ,那么你一定需要定义如下方法:

     func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {...} 

~ func numberOfSectionsInTableView(tableView: UITableView) -> Int {...} is optional as suggested by @rmaddy, but it's a good practice to define it. func numberOfSectionsInTableView(tableView: UITableView) -> Int {...}是@rmaddy建议的,是可选的,但是定义它是一个好习惯。

delegate property is not set on the tableView ? tableView上未设置delegate属性? also tableView:numberOfRowsInSection: should be implemented 还应该执行tableView:numberOfRowsInSection:

you have to set your delegates and declare that you use those protocols 您必须设置您的代表并声明您使用这些协议

in file.m in didLoad 在didLoad中的file.m中

_table.delegate = self;
_table.dataSource = self;

in file.h when you declare the interface you have to add these protocols UITableViewDelegate and UITableViewDataSource 在file.h中,当您声明接口时,必须添加以下协议UITableViewDelegateUITableViewDataSource

Xcode will tell you which methods you must implement to respect the protocol. Xcode会告诉您必须遵守哪种方法才能遵守协议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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