Search code examples
iosswiftxcodeuitableviewdelegates

Swift make wishlist feature


so I want to make this simple wishlist feature for when the user tapped the "heart" button it will add that data from view to wishlist view. just like this :

enter image description here

so when the user tapped that heart button, that movie will show in this wishlist view like this :

enter image description here

now, my question is how do I notify my wishlistVc so that it knows there's a new "wishlist" that the user tapped from the movie list. I have an idea that I should use a delegate, but still, I can't figure out how to implement a delegate in this case.

and I use "var movieList" to store all the data in HomeVc, and my idea is when the user tapped that heart button in tableview, that data that user tapped with will move into my "let wishlist", so i can populate it on my wishlistVC ( but I don't know how to do this so I need help)

so far this is my code :

class DefaultTableViewCell: UITableViewCell {

    @IBOutlet weak var moviePosterImage: UIImageView!
    @IBOutlet weak var movieTitleLabel: UILabel!
    @IBOutlet weak var wishlistButton: UIButton!


    var indexPath: IndexPath!
    var delegate: DefaultTableViewDelegate?
    var wishlistFlag:Bool = false

    override func layoutSubviews() {
        super.layoutSubviews()
        wishlistButton.titleLabel?.text = ""
        
        wishlistButton.addTarget(self, action: #selector(wishlistTapped(_:)), for: .valueChanged)
    }


    @IBAction func wishlistTapped(_ sender: UIButton) {
        wishlistFlag = !wishlistFlag
        delegate?.wishlistTrigger(row: indexPath.row)
        
        if wishlistFlag == true {
            wishlistButton.setImage(UIImage(named: "heart_fill"), for: .normal)
        }else if wishlistFlag == false {
            wishlistButton.setImage(UIImage(named: "heart"), for: .normal)

        }
    }
}

HomeVc (the vc that shows the movie list):

var movieList : [Movie] = []

extension HomeVC: UITableViewDataSource, UITableViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return movieList.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let data = movieList[indexPath.row]
        let cell = tableView.dequeueReusableCell(withIdentifier: "DefaultTableViewCell", for: indexPath) as! DefaultTableViewCell

        cell.indexPath = indexPath

        cell.movieTitleLabel.text = data.title
        cell.moviePosterImage.sd_setImage(with: data.imageUrl)
        cell.delegate = self

        return cell
    }
}

protocol DefaultTableViewDelegate {
    func wishlistTrigger(row: Int)
}

this is my wishlistVc:

let wishlist : [Movie] = []
extension WishlistVc: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return wishlist.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let data = wishlist[indexPath.row]
        let cell = tableView.dequeueReusableCell(withIdentifier: "DefaultTableViewCell", for: indexPath) as! DefaultTableViewCell

        cell.movieTitleLabel.text = data.title
        cell.moviePosterImage.sd_setImage(with: data.imageUrl)
        cell.wishlistButton.titleLabel?.text = ""
        cell.indexPath = indexPath

        return cell
    }
}

I've been stuck for 2 whole days now I still don't know how to figure this out. I appreciate anyone that can help me. Thanks


Solution

  • Implement func like:

    func wishlistTrigger(row: Int) {
        self.myWishlistedItem.append(self.movieList[row]) //Add that wishlisted item in array
       self.tableView.reloadData() //Now reload Table
    }