TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

sending data to table view when tapping a button using delegate pattern

Forums > Swift

I have a collectionView and there is a button on the each cell. and when I tap the button, I'd like to display a table view cell with the data the coooection view cell had usning the delegate pattern. I'd appreciate your help.

this is the collectionViewCell class:

protocol DataTransformationDelegate: AnyObject {

}

class SlotCollectionViewCell: UICollectionViewCell {

@IBOutlet weak var title: UILabel!
@IBOutlet weak var subtitle: UILabel!
@IBOutlet weak var date: UILabel!
@IBOutlet weak var favoriteButton: UIButton!
@IBOutlet weak var thumbnail: UIImageView!
weak var delegate: DataTransformationDelegate?

var favorite = false {
    didSet {
        if favorite {
            favoriteButton.setBackgroundImage(UIImage(named: "favoriteOn"), for: .normal)
        } else {
            favoriteButton.setBackgroundImage(UIImage(named: "favoriteOff"), for: .normal)
        }
    }
}

override func awakeFromNib() {
    super.awakeFromNib()
}

@IBAction func favoriteButtonTapped(_ sender: UIButton) {
    favorite.toggle()
}
}

this is the table view controller that I wanna show the cell that I chose as favorite:

class FavoriteTableViewController: UIViewController {

@IBOutlet weak var tableView: UITableView!
let slotCollectionViewCell = SlotCollectionViewCell()

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.register(UINib(nibName: String(describing: FavoriteTableViewCell.self), bundle: nil), forCellReuseIdentifier: String(describing: FavoriteTableViewCell.self))

    tableView.delegate = self
    tableView.dataSource = self
}

@IBAction func toCollectionViewTapped(_ sender: Any) {
    let storyboard = UIStoryboard(name: String(describing: ThumbnailCollectionViewController.self), bundle: nil)
    let thumbnailCollectionVC = storyboard.instantiateViewController(withIdentifier: "ThumbnailCollectionViewController") as! ThumbnailCollectionViewController
    thumbnailCollectionVC.modalPresentationStyle = .fullScreen
    self.present(thumbnailCollectionVC, animated: true)
}
}

 extension FavoriteTableViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 90
 }
 }

extension FavoriteTableViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return MockData.mockData.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: FavoriteTableViewCell.self), for: indexPath) as! FavoriteTableViewCell

    let currentDateTime = Date()
    let dateFormatter = DateFormatter()
    dateFormatter.timeStyle = .long

    let mockData = MockData.mockData[indexPath.row]
    cell.title.text = mockData.title
    cell.subtitle.text = mockData.subtitle
    cell.date.text = "\(dateFormatter.string(from: currentDateTime))"
    cell.thumbnail.image = UIImage(named: mockData.thumbnail)

    return cell
}
}

3      

you will need to create a function inside the protocol

protocol DataTransformationDelegate: AnyObject {
              func changeFav()
              or may be a property you want to change 
}

then you can create class where you create a function that in turn calls this function in protocoal and performs any action you want

class FavAction{
    weak var delegate:DataTransformationDelegate?
    func  callFavToPlay() {
              delegate?.changeFav()
              // or may be update the property you want to change
    }
}

Now you can use extension to adopt the DataTransformationDelegate protocol in the viewcontroller

Create an instance of FavAction class and call the method callFavToPlay.

As it is called it will trigger, what you have done in

 func  callFavToPlay() {
              delegate.changeFav()
              // or may be update the property you want to change
    }

so you now update or receive a value and use it inside the ViewController which has adopted DataTransformationDelegate

you can the use the value you will receive from this function to update

so this way you can use delegate pattern , when you pass the responsibilty and then when then responsibilty is fulfilled you get a the value in return that you want and use it inside the viewcontroller that adopts that protocol which declares that responsibility .

3      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.