未调用UITableView方法cellForRowAtIndexPath

Bas*_*Bas 16 uitableview swift

UITableView没有显示任何数据.我认为问题在于:

override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell?`
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell? {
    // Configure the cell...
    let cellId: NSString = "Cell"
    var cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(cellId) as UITableViewCell

    if let ip = indexPath {
        //var data: NSManagedObject = myList[ip.row] as NSManagedObject
        //cell.textLabel.text = data.valueForKey("name") as String
        cell.textLabel.text = "Hi"
    }
    return cell
}
Run Code Online (Sandbox Code Playgroud)

我在youtube上看过使用这段代码的教程,但我无法让它工作.

首先我打了它是我的数组的一个问题,但是当我把文本放入其中它仍然没有显示任何东西.

我正在运行Xcode6 beta 3,但是这个代码确实在beta 2中运行得非常好.

我没有任何警告或错误消息.

编辑:我查找了一些其他的Swift项目,我在那里得到了相同的功能,它在2周前工作但现在它也不起作用.也许这是一个Xcode6 beta 3问题?

我的班级是类型UITableViewController.

编辑:我正在浏览Apple开发人员论坛,并在更新到xCode 6 beta 3后看到相关问题,所以它可能是xCode 6 beta 3的问题!

编辑:该方法不被调用.

kur*_*amo 45

得到它了!

有同样的问题 - 我也在uiviewcontroller中添加了一个tableview,它也没有用,但我现在知道这个问题了.

如果在界面构建器上添加tableview并在"添加缺失约束"之后单击,它将添加无法一起工作的约束.

在我的位置,我在我的整个uiview上有了tableview,点击"Add Missing Constraints"后,它会在这里添加:

在此输入图像描述

现在如果你删除其中一个Trailing或Leading Alignments,它将起作用 - cellforrowatindexPath将被调用!不知道为什么会这样,但它会起作用.

希望我能帮助你.


vac*_*ama 11

你需要实现这个或cellForRowAtIndexPath永远不会被调用:

override func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
  // Return the count of the number of rows in the table
  return 5
}
Run Code Online (Sandbox Code Playgroud)

5只是一个例子.您将返回数组中元素数量的计数.

  • 展示如何设置`tableView.dataSource`.在我的Swift项目中,我在``UITableViewController`子类的`viewDidLoad()`中设置了`tableView.dataSource = self`. (3认同)

小智 5

检查您是否有以下几行:

tableView.delegate = self
tableView.dataSource = self
Run Code Online (Sandbox Code Playgroud)

如果dataSource = self未实现,则可能是未调用cellForRowAt indexPath方法的原因