我有两个带有两个不同表视图的视图控制器,其中FirstVC使用segue将数据发送到SecondVC。我的数据来自json文件,一切正常。我在SecondVC 中有一个按钮(称为likeButton),当我按下该按钮以显示“喜欢的图像”,然后再次按下该按钮可以返回正常的图像“不喜欢”。我做到了,并且工作正常。也将数据发送回FirstVC,在该处显示图像为“ like”。我使用了UserDefaults。我的问题是,当您按下按钮在 SecondVC 中显示“ like image”时,在 likeButtonAction(_ sender:UIButton)函数中,它会在每个单元格中被按下。我需要为该特定单元格选择一个。 我还是Xcode的新手。任何帮助都感激不尽。谢谢
这就是我所拥有的: First View Controller
import UIKit
class FirstVC: UIViewController, UITableViewDataSource, UITableViewDelegate {
let defaults = UserDefaults.standard
@IBOutlet weak var tableView: UITableView
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "popDetails" {
guard let vc = segue.destination as? DetailsViewController,
let index = tableView.indexPathForSelectedRow?.row
else {
return
}
vc.cocktailsName = drinks[index].cocktailName
// more data send
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return drinks.count
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
tableView.rowHeight = 260
let cell = tableView.dequeueReusableCell(withIdentifier: "favoriteCell") as! TableViewCell
cell.cocktailLabel.text = drinks[indexPath.row].cocktailName
selectedValue = defaults.value(forKey: "myKey") as! String
if selectedValue == "true" {
cell.heartImage.image = UIImage(named: "heart")
} else if selectedValue == "false" {
cell.heartImage.image = UIImage(named: "transparent")
}
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
DispatchQueue.main.async {
self.performSegue(withIdentifier: "popDetails", sender: self)
}
}
}
第二视图控制器:
var selectedValue = String()
class SecondVC: UIViewController, UITableViewDataSource, UITableViewDelegate, UITextViewDelegate {
var cocktailsName: String!
@IBOutlet weak var tableView: UITableView!
var mainCocktails: Cocktails!
var tap = UITapGestureRecognizer()
var defaults = UserDefaults.standard
var checked = false
@IBAction func done(_ sender: AnyObject) {
dismiss(animated: true, completion: nil)
}
@IBAction func likeButtonAction(_ sender: UIButton) {
if checked {
sender.setImage(UIImage(named:"likeButton"), for: UIControl.State.normal)
checked = false
} else {
sender.setImage(UIImage(named:"likeButtonOver"), for: UIControl.State.normal)
checked = true
}
selectedValue = String(checked)
defaults = UserDefaults.standard
defaults.set(selectedValue, forKey: "myKey")
defaults.synchronize()
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "buttonsCell") as! MainPictureViewCell
cell.nameLabel.text = cocktailsName
selectedValue = self.defaults.value(forKey: "myKey") as! String
if selectedValue == "true" {
cell.likeButton.setImage(UIImage(named: "likeButtonOver"), for: .normal)
} else if selectedValue == "false" {
cell.likeButton.setImage(UIImage(named: "likeButton"), for: .normal)
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 4
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
}
答案 0 :(得分:0)
您将按钮放在单元格中,并在cellController中放置插座。然后,必须在cellForRowAt中将目标添加到按钮,并为按钮提供indexpath.row的标签
extension ViewController: UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TableCell") as! TableCell
cell.button.addTarget(self, action: #selector(buttonPressed), for: .touchUpInside)
cell.button.tag = indexPath.row
return cell
}
}
//MARK:- Handel Button
@objc func buttonPressed(sender:UIButton){
print(sender.tag)
}