Search code examples
swiftcollectionview

How to pass a selected state of the cell in collectionView (Swift)


I have an app with horizontal scroll collectionView and I need to pass the "selected" state to other method, which is saving. So I have custom cell in which I declare an observer:

class StoryViewCell: UICollectionViewCell {

    override var isSelected: Bool {
        didSet {
            print("Selected")
        }
    }

}

When I scroll, I got this print statements like it should be. But how can I pass this state to my ViewController? I can't access variables inside it. Are there other options to solve this?

I also tried some collectionView delegate's such as :didSelectItem and :didUpdateFocusIn context methods with no luck.

Any help is appreciated


Solution

  • We have many ways to do that so one of them:

    You have to create protocols:

    protocol updateDelegate {
        func update(flag: Bool)
    }
    

    And create a delegate object in cell class like that:

     var delegate: updateDelegate?
    

    After that, Your cellForRowAt:

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! DemoTableViewCell
    
        cell.delegate = self
        return cell
    }
    

    And cell class methods like that:

    override var isSelected: Bool {
        didSet {
            print("Selected")
            delegate?.update(flag: isSelected)
        }
    }
    

    And in view controller you have to define update method:

    func update(flag: Bool) {
        print("fefr")
    }
    

    It will work for you

    You can find my full code below:

    ViewControllor:

    import UIKit
    
    protocol updateDelegate {
        func update(flag: Bool)
    }
    class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, updateDelegate {
    
    
        @IBOutlet weak var tableView: UITableView!
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return 5
        }
    
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
            let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! DemoTableViewCell
    
            cell.delegate = self
            return cell
        }
        func update(flag: Bool) {
            print("fefr")
        }
    }
    

    Custom cell class

    class DemoTableViewCell: UITableViewCell {
    
         var delegate: updateDelegate?
    
        override func awakeFromNib() {
            super.awakeFromNib()
            // Initialization code
        }
    
        override func setSelected(_ selected: Bool, animated: Bool) {
            super.setSelected(selected, animated: animated)
    
            // Configure the view for the selected state
        }
        override var isSelected: Bool {
            didSet {
                print("Selected")
                delegate?.update(flag: isSelected)
            }
        }
    
    }