swift - 自定义委托(delegate)方法正在调用

标签 swift custom-cell

import UIKit
protocol CustomCellDelegate: class {
func liked(dataDict:NSDictionary,index:NSInteger)
}
class NameImageTextCell: UITableViewCell,UIActionSheetDelegate {
weak var delegateCell: CustomCellDelegate?
@IBAction func btnAction(_ sender: UIButton) {
      if delegateCell==nil{
            delegateCell?.liked(dataDict: dataNodeDict, index: ItemIndex)
        }    
  }
}




////////
 class 
 FanWallVC:UIViewController,UITableViewDelegate,UITableViewDataSource,
 CustomCellDelegate {
 @IBOutlet weak var tableView: UITableView!
 let objNameImageTextCell = NameImageTextCell()
 override func viewDidLoad() {
    super.viewDidLoad()
    tableView.register(UINib(nibName: "NameImageTextCell", bundle: nil), 
  forCellReuseIdentifier: "NameImageTextCell")
    objNameImageTextCell.delegateCell=self
   }
  func liked(dataDict: NSDictionary, index: NSInteger) {
   print("Called")
   }
  }

当我单击NameImageTextCell中的IBAction(btnAction)时,delegateCell为零, 所以喜欢的方法没有被调用。 请帮我。 提前致谢

最佳答案

您可能应该调用:

objNameImageTextCell.delegateCell = self

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

}

所以它应该看起来像这样:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "NameImageTextCell", for: indexPath) as! NameImageTextCell

        cell.delegateCell = self
        //other cell customization

        return cell

    }

关于swift - 自定义委托(delegate)方法正在调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44064773/

相关文章:

苹果手机 : Use table custom cell

swift - 为什么 IQKeyboardManager 不滚动我的 UITableView 以使我的自定义单元格的文本字段可见

ios - 将对象数组转换为字符串数组

ios - Alamofire DownloadRequest 验证并从服务器获取响应数据

swift - 如何在可以覆盖的类级别存储常量?

ios - 针对数组测试 TableView 的单元格中包含的值

swift - 分段控件中自定义 UIView 内的 UITableView 不显示自定义单元格中的 JSON 值

swift - 使用变量作为键访问 Swift 字典

Swift 协议(protocol)可通过只读属性设置属性

uitableview - UITableViewCells 的不同 - 重复模式