我想将 secondTag
添加到 UITableView
作为存储属性。
但是当我尝试扩展 UITableView
并像这样使用我的 CustomTableView
时:
func tableView(_ tableView: CustomTableView, numberOfRowsInSection section: Int) -> Int
tableView 协议(protocol)(UITableViewDelegate
、UITableViewDataSource
)给我一个错误,因为我没有遵循它的要求。
将 secondTag
属性添加到 UITabelView
有哪些选择?
我应该覆盖 tableView 协议(protocol)吗?
我是否应该创建一个自定义 tableView UITableViewDataSource
,它的方法接受我的 CustomTableView
作为函数的参数类型?
最佳答案
在实现协议(protocol)方法时,方法签名需要与协议(protocol)中定义的类型相匹配。协议(protocol)方法期望 UITableView
,因此实现该函数签名,然后在函数内部转换为您的类型:
func tableView(_ tableView: UITableView , numberOfRowsInSection section: Int) -> Int {
guard let tableView = tableView as? CustomTableView else { return 0 }
// now you can access tableView.secondTag
}
关于ios - 需要帮助将新的存储属性添加到 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56450470/