自定义委托方法正在调用

时间:2017-05-19 08:10:27

标签: 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为零, 所以喜欢的方法是没有接到电话。 请帮我。 提前致谢

1 个答案:

答案 0 :(得分:2)

您可能应该致电:

objNameImageTextCell.delegateCell = self

in

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

    }