1

I am creating a custom delegate in my TableViewCell class and calling it in tableView class, but don't know why this error is coming.

TableView Class :-

 //Cell For Row at indexPath
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    if (0 == indexPath.section)
    {
        let cell = tableView.dequeueReusableCell(withIdentifier: "FirstRowCell") as! FirstRowCell
        cell.btnReview.addTarget(self, action: #selector(GotoReview), for: UIControlEvents.touchUpInside)

        //cell.
        return cell
    }

    else if ( 1 == indexPath.section)
    {
        let identifier = "TableCollectionCell"
        var tableCollectionCell = tableView.dequeueReusableCell(withIdentifier: identifier)
        if(tableCollectionCell == nil)
        {
            let nib:Array = Bundle.main.loadNibNamed("TableCollectionCell", owner: self, options: nil)!
            tableCollectionCell = nib[0] as! TableCollectionCell

            //Delegate
            tableCollectionCell.delegate = self

        }

        return tableCollectionCell!
    }

    else
    {
        let identifier = "BrandImagesTableCell"
        var brandImagesTableCell = tableView.dequeueReusableCell(withIdentifier: identifier)
        if(brandImagesTableCell == nil)
          {
            let nib:Array = Bundle.main.loadNibNamed("BrandImagesTableCell", owner: self, options: nil)!
            brandImagesTableCell = nib[0] as? BrandImagesTableCell
          }
        return brandImagesTableCell!
    }
}


extension HomeViewController : CollectionViewCellTap
{
  func didTapCollectionViewCell(indexPath: Int) {
    let gotoCreateGroup =  self.storyboard?.instantiateViewController(withIdentifier: "CreateGroupView") as! CreateGroupView

    self.present(gotoCreateGroup, animated: true, completion: nil)
  }
}

Calling from TableViewCell class :-

  protocol CollectionViewCellTap
  {
   func didTapCollectionViewCell(indexPath : Int)

  }

  class TableCollectionCell: UITableViewCell {

@IBOutlet weak var collectionView: UICollectionView!

var delegate : CollectionViewCellTap?

override func awakeFromNib() {
    super.awakeFromNib()

    //collectionView cell
    collectionView!.register(UINib(nibName: "CreateGroupCell", bundle: nil), forCellWithReuseIdentifier: "CreateGroupCell")
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

}

 }

  //Collection View
 extension TableCollectionCell : UICollectionViewDataSource ,UICollectionViewDelegateFlowLayout
 {
    func collectionView(_ collectionView: UICollectionView,  numberOfItemsInSection section: Int) -> Int
{
    return 4
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CreateGroupCell", for: indexPath) as! CreateGroupCell

    cell.groupName.text = ""
    CreateCardView(viewCorner: cell.cardView)

    if(0 == indexPath.row)
    {
        //cell.btnDotted.tag = indexPath.row
        //cell.btnShare.tag = indexPath.row
        cell.btnDotted.setImage(UIImage(named: "Info"), for: UIControlState.normal)
        cell.btnShare.setImage(UIImage(named : "AddGroup"), for: UIControlState.normal)
    }

    else if(indexPath.row >= 1 && indexPath.row <= 3)
    {
        cell.btnDotted.isHidden = true
        cell.btnShare.isHidden = true
        cell.groupImage.image = UIImage(named: "group_dull_card.png")
    }

    else
    {

    }

    return cell
}


func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    if ( 0 == indexPath.row) {

       delegate?.didTapCollectionViewCell(indexPath: indexPath.row)
    }
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

    return CGSize(width: 200, height: 150)
}
}

I am new in iOS.

halfer
  • 19,824
  • 17
  • 99
  • 186
Kishor Pahalwani
  • 1,010
  • 1
  • 23
  • 53

2 Answers2

1

Check This -

I think you are missing this

enter image description here

iDeveloper
  • 2,339
  • 2
  • 24
  • 38
1

You need to explicitly specify the type of CustomTableCell when you are initiating cell object with dequeueReusableCell(withIdentifier:) other wise it consider as UITableViewCell's object that doesn't have property delegate, so simply add as! TableCollectionCell at the end, so now you are able to access the property of class TableCollectionCell.

var tableCollectionCell = tableView.dequeueReusableCell(withIdentifier: identifier) as? TableCollectionCell 
Nirav D
  • 71,513
  • 12
  • 161
  • 183