Swift UITableViewDelegate方法不被调用

Swift UITableViewDelegate方法不被调用

问题描述:

我创建了一个ViewController并使用storyboard向其添加TableViewSwift UITableViewDelegate方法不被调用

我将Storyboard中的Class设置为我的NewsFeedViewController类的ViewController。我将tableview插座的委托设置为self。

class NewsFeedViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 

    @IBOutlet weak var newsFeedTableView: UITableView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     newsFeedTableView.delegate = self 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 
    func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     return 1 
    } 

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

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = 
     newsFeedTableView.dequeueReusableCellWithIdentifier(
      "newsFeedIphoneCell", forIndexPath: indexPath) 
      as! NewsFeedIphoneTableViewCell 

     return cell 
    } 

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 

    } 

} 

当我运行代码时,没有一个委托方法被触发。我究竟做错了什么?

您缺少数据来源的分配。

尝试增加

newsFeedTableView.dataSource = self 

为好。

+1

真棒,帮了我很多! –