Search code examples
swiftcustom-cell

Custom delegate method is calling


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")
   }
  }

When i Click on IBAction(btnAction) in NameImageTextCell, delegateCell is nil, So liked method is not getting call. Please help me. Thanks in advance


Solution

  • You probably should call:

    objNameImageTextCell.delegateCell = self
    

    in

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

    so it should look somthing like:

    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
    
        }