繁体   English   中英

在FirstVC中选择单元格后如何为SecondVC中的每个单元调用按钮操作?

[英]How to call button action for each cell in SecondVC after selecting cell in FirstVC?

我有两个具有两个不同表视图的视图控制器,其中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
    }
}

您在单元格中放置按钮并在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)

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM