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



我想添加一个secondTag作为存储属性UITableView

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

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

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

secondTag属性添加到UITabelView有哪些选项?我应该覆盖表视图协议吗?我是否应该创建一个自定义表视图,UITableViewDataSource其方法接受我的CustomTableView作为函数的参数类型?

实现协议方法时,方法签名需要与协议中定义的类型匹配。 协议方法需要UITableView,因此实现该函数签名,然后强制转换为函数内部的类型:

func tableView(_ tableView: UITableView , numberOfRowsInSection section: Int) -> Int {
    guard let tableView = tableView as? CustomTableView else { return 0 }
    // now you can access tableView.secondTag
}

最新更新