Ios 类型为'的值;自定义表格单元格';没有成员代表
我有一个名为CustomTableCell的UITableViewCell子类,它在swift 4.1中声明。文件 在我的视图控制器和我在cellForRowAt中拥有的UITableView中:Ios 类型为'的值;自定义表格单元格';没有成员代表,ios,iphone,swift,ios11,swift4.1,Ios,Iphone,Swift,Ios11,Swift4.1,我有一个名为CustomTableCell的UITableViewCell子类,它在swift 4.1中声明。文件 在我的视图控制器和我在cellForRowAt中拥有的UITableView中: let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier") as! CustomTableCell cell.delegate = self 我得到以下错误: Value of type 'CustomTab
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有您的自定义委托
,则只需要分配它。因此,如果您的CustomTableCell
没有antCustom委托,请删除该行
对于tableView委托方法,必须将其添加到viewDidLoad()中:
或者仅使用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)")
}
}
自定义单元格是否具有名为delegate的属性?它可能缺少声明。cell.delegate
!=<代码>UITableViewDelegate
。单元格必须有自己的委托协议。为什么否定和结束与编程无关?好奇的是,如何在CustomTableCell类中创建单元格委托?只是curious@cdub更新了我的答案以显示自定义委托的示例。请确保使用“委托?****”(如果您不确定委托是否为零)或“委托!****”(如果您确定它不是零)作为可选项。@这很好,因为我忘了添加它,因为我直接在这里键入它。更新了。@SharadChauhan我想。和按钮前面的“func”。
extension ViewController: CustomCellDelegate {
func someFunctionToPassSomeValue(name: String) {
print("Delegate is working. Value : \(name)")
}
}