swift - 访问 tableView :cellForRowAtIndexPath

标签 swift uitableview uiviewcontroller

1.在Storyboard 中创建一个空白的新View Controller。将其绑定(bind)到自定义类 ViewController

2.将Table View拖到View Controller中。

3.在ViewController的代码中继承UITableViewDataSource

现在我在ViewController中使用函数tableView:cellForRowAtIndexPath,但是它不能被标记为override,显然这个函数不起作用,因此此 tableView 仅显示空白。

我已经正确地为表格 View 单元格设置了 View 和自定义类,我认为这在这里无关紧要。

我错过了什么?

最佳答案

你必须继承 UITableViewDelegate,在代码中将 Tableview 拖到 Storyboard 中,然后在 viewDidLoad 方法中添加以下行:

tableView.datasource = self
tableView.delegate = self

此外,cellForRowAtIndexPath 可能不会被调用,因为以下一个(或多个)原因:

numberOfRowsInSection 返回 0

numberOfSectionsInTableView 返回 0

数据源未正确设置

heightForRowAtIndexPath 返回 0

表格的高度和/或宽度的框架大小为 0

关于swift - 访问 tableView :cellForRowAtIndexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34407725/

相关文章:

objective-c - 如何设置部分 curl 模式不隐藏工具栏?

ios - 多个 UIViewController 的单个背景图像

swift - Swift 3.0 中的可玩游戏区

ios - 描述 SKPhysicsContact 对象(属性和方法)

ios - 在 Storyboard 中创建自定义 UITableview 节标题的最佳方法

ios - 如何从单个tableViewcell中获取多个按钮?

swift - 如何在常数时间内返回下标?

swift - 从 Swift 应用程序打开日历

ios - 删除 tableView 单元格

ios - 模态关闭后,键盘显示错误的颜色 - iOS