所以我想制作這個簡單的心愿單功能,當用戶點擊“心形”按鈕時,它會將資料從視圖添加到心愿單視圖。像這樣 :

因此,當用戶點擊該心形按鈕時,該電影將顯示在此心愿單視圖中,如下所示:

現在,我的問題是如何通知我的 WishlistVc,以便它知道用戶從電影串列中點擊了一個新的“愿望清單”。我有一個想法,我應該使用委托,但仍然無法弄清楚如何在這種情況下實作委托。
我使用“var movieList”將所有資料存盤在 HomeVc 中,我的想法是當用戶在 tableview 中點擊那個心形按鈕時,用戶點擊的資料將移動到我的“let Wishlist”中,所以我可以填充它我的 WishlistVC(但我不知道該怎么做,所以我需要幫助)
到目前為止,這是我的代碼:
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(顯示電影串列的vc):
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)
}
這是我的心愿單Vc:
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
}
}
我已經被困了整整 2 天,現在我仍然不知道如何解決這個問題。我感謝任何可以幫助我的人。謝謝
uj5u.com熱心網友回復:
實作 func 像:
func wishlistTrigger(row: Int) {
self.myWishlistedItem.append(self.movieList[row]) //Add that wishlisted item in array
self.tableView.reloadData() //Now reload Table
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/388635.html
