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/