【发布时间】:2022-01-21 20:40:46
【问题描述】:
所以我想做这个简单的愿望清单功能,当用户点击“心”按钮时,它会将数据从视图添加到愿望清单视图。就像这样:
因此,当用户点击该心形按钮时,该电影将显示在此愿望清单视图中,如下所示:
现在,我的问题是如何通知我的wishlistVc,以便它知道用户从电影列表中点击了一个新的“wishlist”。我有一个想法,我应该使用委托,但我仍然不知道在这种情况下如何实现委托。
我使用“var movieList”将所有数据存储在 HomeVc 中,我的想法是当用户点击 tableview 中的那个心形按钮时,用户点击的数据将移动到我的“让愿望清单”中,所以我可以将其填充到我的 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
}
}
我已经被困了整整两天了,我仍然不知道如何解决这个问题。我感谢任何可以帮助我的人。谢谢
【问题讨论】:
-
wishlistTrigger(row: Int)func 的实现在哪里 -
而在
HomeVC中你必须实现wishlistTrigger(row: Int)func。 -
@Kudos 是的,我不知道在wishlistTrigger 里面放什么,所以我没有真正在我的代码中实现它
-
请检查我的答案..
标签: ios swift xcode uitableview delegates