1

I want to show/hide collection view inside the table view's check button selection

I am trying to use delegate and protocol for that.

1.I create protocol in table view cell class

   protocol CustomCellDelegate{
   func selectCollectionView(cell: InsideTableViewCell)
   }

Note:I create selectCollectionView function inside the main view controller

2.declare delegate variable inside the main view class

var delegate: CustomCellDelegate?

3.Confirm to the CustomCellDelegate in the main class

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,UICollectionViewDelegate,UICollectionViewDataSource,CustomCellDelegate

4.Use selector function to provide table view button inside the cellForRowAtIndexPath function

cell.checkButton.addTarget(self, action: #selector(self.selectCheck(_:)), for: .touchUpInside)
cell.delegate = self

5.Selector function

 @objc func selectCheck(_ sender: UIButton) {

    if sender.isSelected {
        sender.isSelected = false
        print("Check 1")

    delegate?.selectCollectionView(cell: InsideTableViewCell)

    } else{
        print("Check 2")
        sender.isSelected = true

    }


}

6.selectCollectionView function

func selectCollectionView(cell: InsideTableViewCell) {

    cell.clCollectionView.isHidden = true
}

I am trying to call selectCollectionView function inside the selectCheck button

but I get error like "Cannot convert value of type 'InsideTableViewCell.Type' to expected argument type 'InsideTableViewCell'"

If I do mistake please let me know.

I referred this link:How to access the content of a custom cell in swift using button tag?

And Screenshot of output:

enter image description here

Vinayak Bhor
  • 691
  • 1
  • 8
  • 20
  • 1
    Change `delegate?.selectCollectionView(cell: InsideTableViewCell)` to `delegate?.selectCollectionView(cell: anActualCell)` – matt May 26 '18 at 11:50
  • Not relsolved using InsideTableViewCell @matt this is actual cell I tried with self also but still getting same error – Vinayak Bhor May 26 '18 at 12:02
  • 1
    The issue is that `InsideTableViewCell`is a class, not an object. You need to retrieve the cell (object) in that method. `self` won't work in your case because it's a `ViewController` object. put the selector code inside the cell.swift code and it then you can use `self` correctly. Just set the delegate to the viewcontroller. – Larme May 26 '18 at 15:27
  • @Larme reloved the issue.Thank you so much. – Vinayak Bhor May 27 '18 at 15:51

0 Answers0