<分区>
我在我的项目中使用 UITableView,但在我的类(class)中没有 numberOfRowsInSection
,但类(class)给出了这个错误。类型“ViewController”不符合协议(protocol)“UITableViewDataSource”。您想添加协议(protocol) stub 吗?如何解决这个问题?
<分区>
我在我的项目中使用 UITableView,但在我的类(class)中没有 numberOfRowsInSection
,但类(class)给出了这个错误。类型“ViewController”不符合协议(protocol)“UITableViewDataSource”。您想添加协议(protocol) stub 吗?如何解决这个问题?
最佳答案
在UITableViewDataSource
协议(protocol)下,有一些提供默认值的函数。
例如,optional func numberOfSections(in tableView: UITableView) -> Int
方法是可选的,它提供返回 1
的默认实现。
但是,该协议(protocol)包含两个非可选的方法,它们必须被实现。它们是:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
和
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
以下是这些功能的入门指南:
class ViewController: UIViewController {
private let data = [0, 1, 2, 3, 4]
}
extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
return tableView.dequeueReusableCell(withIdentifier: "identifier", for: indexPath)
}
}
这是协议(protocol)的官方文档: https://developer.apple.com/documentation/uikit/uitableviewdatasource
关于ios - ( numberOfRowsInSection ) ' Type ' ViewController' 不符合协议(protocol) 'UITableViewDataSource',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57643402/
相关文章:
ios - Objective-c setContentOffset 方法
ios - 如何将 indexPath 值从 performSegue() 传递到 prepareForSegue()
ios - 为什么某些对象不能与 NSDictionary 速记一起使用?
ios - sigabrt 错误在 Swift 中以编程方式调用 segue
iOS 用户界面测试 : Get image filename (Swift)
objective-c - 如何从 tableView :cellForRowAtIndexPath? 中访问 tableView 单元格的高度
ios - 关于 UIViewController 如何使用 UIViewcontroller 的属性的扩展协议(protocol)