ios - “CustomTableCell”类型的值没有成员委托
问题描述
我有一个名为 CustomTableCell 的 UITableViewCell 子类,它在 swift 4.1 中声明。文件。
在我的带有 UITableView 的视图控制器中,我在 cellForRowAt 中有:
let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier") as! CustomTableCell
cell.delegate = self
我收到以下错误:
Value of type 'CustomTableCell' has no member delegate.
我在顶部声明了 UITableViewDelegate。
解决方案
UITableViewDelegate 不需要cell.delegate = self
.
如果您 CustomTableCell 有您的Custom Delegate
,那么您只需要分配它。因此,如果您没有蚂蚁Custom Delegate
,请CustomTableCell
删除该行。
对于 tableView 委托方法,您必须在 viewDidLoad() 中添加:
yourTableView.delegate = self
yourTableView.dataSource = self
或仅使用连接它StoryBorad
。
回答:如何在 CustomTableCell 类中创建单元格委托?只是好奇
CustomTableCell.swift :
// Custom protocol
protocol CustomCellDelegate: NSObjectProtocol {
// Protocol method
func someFunctionToPassSomeValue(name: String)
}
class CustomTableCell: UITableVieCell {
weak var delegate: CustomCellDelegate?
// Your class implementations and outlets..
//Call your protocol method in some action, for example in button action
@IBAction func buttonAction(sender: UIButton) {
delegate?.someFunctionToPassSomeValue(name: "anyStringValue")
}
}
然后在ViewController
课堂上,您需要将实例分配给自定义委托变量。
let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier") as! CustomTableCell
cell.delegate = self
并实现协议方法:
extension ViewController: CustomCellDelegate {
func someFunctionToPassSomeValue(name: String) {
print("Delegate is working. Value : \(name)")
}
}
推荐阅读
- java - 骆驼路线发送数据后关闭Spring应用程序
- linux - 在阻止读取调用时从文件系统访问rest api获取动态文件内容
- python - 使用掩码和多个映射替换列值
- azure-functions - 多次查询 CosmosDB 的函数
- python - 我可以在 for 循环中扩展范围吗?
- python - 读取和写入 CSV 文件
- typo3 - TYPO3 新闻扩展 - 我如何复制和粘贴新闻条目的内容元素?
- routes - Ubuntu 16.04 Hotplug Usb Dongle 和以太网,同时保持以太网作为首选接口
- ansys - ANSYS中的节点如何划分?
- python - 我想通过 Robot Framework 4.0 XML 库在 XML 中设置值