簡體   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