Как я могу передать данные через segue из collectionViewCell и кнопки, встроенной в TableViewCell? - PullRequest
1 голос
/ 19 июня 2019

Я хочу передать данные через segue из collectionViewCell, встроенного в TableViewCell; Также из Button, встроенного в TableViewCell

Вот пример кода:

Вот класс TableViewCell's:

class PopularCell: UITableViewCell {

    @IBOutlet weak var ViewAllButton: UIButton!


    @IBOutlet weak var PopularEvents: UILabel!


    @IBOutlet weak var EventCollection: UICollectionView! // EMBEDDED COLLECTIONVIEW

    var events = [Events]()


    override func awakeFromNib() {
        super.awakeFromNib()
        // EMBEDDED BUTTON
        ViewAllButton.setIcon(prefixText: "View All ", prefixTextColor: .blue, icon: .typIcons(.chevronRight), iconColor: .blue, postfixText: " ", forState: .normal, iconSize: 24) // EMBEDDED BUTTON

        EventCollection.delegate = self
        EventCollection.dataSource = self
    }

    extension PopularCell: UICollectionViewDelegate, UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            return events.count
   }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = EventCollection.dequeueReusableCell(withReuseIdentifier: "EventCell", for: indexPath) as! EventCell
            let event = events[indexPath.row]
            print("Event Name:\(event.event_name)")
            cell.event = event
            return cell

    }
}

Вот это ViewController(TableView Delegate & DataSource) class:

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return groupedEventArray.count
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 245
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "PopularCell", for: indexPath) as! PopularCell
        let (category, events) = groupedEventArray[indexPath.row]

        cell.ViewAllButton.addTarget(self, action: #selector(VenueViewController.ViewAll(_:)), for: .touchUpInside)

        cell.PopularEvents.text = category
        cell.events = events

        return cell
    }

    @objc func ViewAll(_ sender:UIButton!) {
        self.performSegue(withIdentifier: "ViewEvents", sender: sender)
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if(segue.identifier == "ViewEvents") {
            let destination = segue.destination as? EventListController
            destination?.navigationItem.title = "Event Category"
        }

        else if(segue.identifier == "ViewEventDetails") {

            if let collectionCell: EventCell = sender as? EventCell {
                if let _: UICollectionView = collectionCell.superview as? UICollectionView {
                    let destination = segue.destination as? EventDetailViewController
                    destination?.navigationItem.title = "Event Details"
                    }
                }
            }

        }

Как настроить func prepare для передачи данных через "ViewEvents" & "ViewEventDetails" segue идентификаторов соответственно

...