cellForRowAtIndexPath无法正常工作

时间:2016-02-18 16:45:19

标签: ios swift uitableview

我的应用程序中有一个tableView,当用户点击该单元格时,我需要它转到另一个视图控制器,该控制器提供有关他们单击的单元格的更多详细信息。当我尝试使用

获取cellForRowAtIndexPath时
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        print("You selected cell #\(indexPath.row)!")

    }

没有任何内容打印到控制台,因此我无法通过segue将该数据传递给另一个视图控制器而不知道这一点。这有什么原因不起作用吗?

我通过此数组获取信息:var postsArray = [PFObject]()并使用return postsArray.count填充表格 任何帮助将不胜感激。我可能会忽略它,但我不确定为什么这不会起作用,因为它在我的其他应用程序中起作用。

我也导入了委托,并且还为我的viewDidLoad方法调用了table.delegate = self

class Profile: UIViewController, UITableViewDelegate, UITableViewDataSource

完整代码可在此处找到:http://pastebin.com/26frZGpa

1 个答案:

答案 0 :(得分:1)

该行

tableView.delegate = self 

缺少第一条评论中的建议,这就是没有调用委托方法的原因。您也可以在Interface Builder中连接该委托。

在您执行segue的情况下,将segue连接到表视图单元而不是视图控制器更方便。

主要的好处是表格视图单元格作为sender中的prepareForSegue参数传递,您可以轻松获取索引路径

  override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
      let selectedIndexPath = tableView.indexPathForCell(sender as! UITableViewCell)!
  }