ios - 需要帮助将新的存储属性添加到 UITableView

标签 ios swift uitableview

我想将 secondTag 添加到 UITableView 作为存储属性。

但是当我尝试扩展 UITableView 并像这样使用我的 CustomTableView 时:

func tableView(_ tableView: CustomTableView, numberOfRowsInSection section: Int) -> Int

tableView 协议(protocol)(UITableViewDelegateUITableViewDataSource)给我一个错误,因为我没有遵循它的要求。

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/

相关文章:

ios - 如何在 Swift 中将标签和 UIButton 添加到 tableViewSection header ?

ios - 向从 Storyboard 构建的 UITableView 添加行

ios - 为什么仍然出现 "Could not cast value of type"错误?

ios - 如果目录不存在,使用 NSFileManager 返回

IOS9 ContactsUI 具体情况

ios - NSSortDescriptor 与关系

ios - GL套件。如何分配给 GLKVector3 的组件

ios - 委托(delegate)未在 Swift 中创建

ios - 即使它是单个事件,我是否应该删除 Firebase 观察者?

ios - 奇怪且令人困惑的 Xcode 断点