简体   繁体   中英

How do I segue when I have multiple Collection views embedded in a Tableview?

I'm having an issue with getting access to the collectionview.tag when doing the prepare (for segue). I have tried different things but nothing is working.

import UIKit

class DiscoverViewController: UIViewController {


let SectionHeaderHeight: CGFloat = 30

@IBOutlet weak var tableView: UITableView!


var tmdbClient = TmdbClientService()
var tableSectionTitles = ["Coming Movies","Playing"," Popular","Toprated"]
var upComing : [Movie]?
var nowPlaying : [Movie]?
var populer : [Movie]?
var topRated : [Movie]?



override func viewDidLoad() {
    super.viewDidLoad()

    fetchMovieTable()
}

func fetchMovieTable(){

    tmdbClient.getUpComing { (movies) in
        self.upComing = movies
        self.tableView.reloadData()
    }
    tmdbClient.getNowPlaying { (movies) in
        self.nowPlaying = movies
        self.tableView.reloadData()
    }
    tmdbClient.getPopuler { (movies) in
        self.populer = movies
        self.tableView.reloadData()
    }
    tmdbClient.getTopRated { (movies) in
        self.topRated = movies
        self.tableView.reloadData()
    }

}




}



    //-MARK: TableView Delegate & Datasource

    extension DiscoverViewController : UITableViewDelegate, UITableViewDataSource {

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

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return 1

    }

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {

        return tableSectionTitles[section]

    }

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

        if section == 0 {

            return nil

        }else {

            let cell = tableView.dequeueReusableCell(withIdentifier: "sectionCell") as! SectionTableViewCell
            cell.setupSection(sectionTitle: tableSectionTitles[section])

            return cell

        }
    }

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {

        if section == 0 {
            return CGFloat.leastNormalMagnitude
        }
        return SectionHeaderHeight
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.section == 0{
            if indexPath.row == 0 {
                let cell = tableView.dequeueReusableCell(withIdentifier: "HeaderTableViewCell") as! HeaderTableViewCell
                cell.headerCollectionView.tag = indexPath.section
                return cell
            }
        }else if indexPath.section == 1{
            if indexPath.row == 0 {
                let cell = tableView.dequeueReusableCell(withIdentifier: "ContentTableViewCell") as! ContentTableViewCell
                cell.contentCollectionView.tag = indexPath.section
                return cell
            }
        }else if indexPath.section == 2{
            if indexPath.row == 0 {
                let cell = tableView.dequeueReusableCell(withIdentifier: "ContentTableViewCell") as! ContentTableViewCell
                cell.contentCollectionView.tag = indexPath.section
                return cell
            }
        }else if indexPath.section == 3{
            if indexPath.row == 0 {
                let cell = tableView.dequeueReusableCell(withIdentifier: "ContentTableViewCell") as! ContentTableViewCell
                cell.contentCollectionView.tag = indexPath.section

                return cell
            }
        }

        return UITableViewCell()
    }
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        if indexPath.section == 0 {
            if indexPath.row == 0 {
                if let cell = cell as? HeaderTableViewCell {
                    cell.headerCollectionView.dataSource = self
                    cell.headerCollectionView.delegate = self
                    cell.headerCollectionView.reloadData()
                }
            }

        }else if indexPath.section == 1{
            if indexPath.row == 0 {
                if let cell = cell as? ContentTableViewCell {
                    cell.contentCollectionView.dataSource = self
                    cell.contentCollectionView.delegate = self
                    cell.contentCollectionView.reloadData()
                }
            }
        }else if indexPath.section == 2{
            if indexPath.row == 0 {
                if let cell = cell as? ContentTableViewCell {
                    cell.contentCollectionView.dataSource = self
                    cell.contentCollectionView.delegate = self
                    cell.contentCollectionView.reloadData()
                }
            }
        }else if indexPath.section == 3{
            if indexPath.row == 0 {
                if let cell = cell as? ContentTableViewCell {
                    cell.contentCollectionView.dataSource = self
                    cell.contentCollectionView.delegate = self
                    cell.contentCollectionView.reloadData()
                }
            }
        }
    }
}

//-MARK: CollectionView Delegate & Datasource

extension DiscoverViewController : UICollectionViewDelegate, UICollectionViewDataSource {

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if collectionView.tag == 0{
            if let upComingMovies = upComing{
                return upComingMovies.count
            }
        }
        if collectionView.tag == 1 {
            if let nowPlayingMovies = nowPlaying{
                return nowPlayingMovies.count
            }
        }
        if collectionView.tag == 2 {
            if let populerMovies = populer{
                return populerMovies.count
            }
        }
        if collectionView.tag == 3 {
            if let topRatedMovies = topRated{
                return topRatedMovies.count
            }
        }
        return 0
    }


    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if collectionView.tag == 0{

            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "HeaderCollectionViewCell", for: indexPath) as! HeaderCollectionViewCell

            cell.movie = upComing?[indexPath.row]

            return cell
        }
        if collectionView.tag == 1 {

            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ContentCollectionViewCell", for: indexPath) as! ContentCollectionViewCell

            cell.movie = nowPlaying?[indexPath.row]
            return cell

        }
        if collectionView.tag == 2 {

            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ContentCollectionViewCell", for: indexPath) as! ContentCollectionViewCell
            cell.movie = populer?[indexPath.row]
            return cell
        }
        if collectionView.tag == 3 {

            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ContentCollectionViewCell", for: indexPath) as! ContentCollectionViewCell
            cell.movie = topRated?[indexPath.row]
            return cell
        }
        return UICollectionViewCell()
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

        print("Collection view at row \(collectionView.tag) selected index path \(indexPath.item)")

        self.performSegue(withIdentifier: "showDiscover", sender: nil)

    }
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {


    }
}

You can pass the collectionview object in self.performSegue as:

 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    print("Collection view at row \(collectionView.tag) selected index path \(indexPath.item)")

    self.performSegue(withIdentifier: "showDiscover", sender: collectionView)

}

Then in prepareSegue:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
       let collectionView = sender as! UICollectionView
       if collectionView.tag == 0 {
                //write your logic
       }
       if collectionView.tag == 1 {
                //write your logic
       }



}

This will work

Create Struct or Class similar to below

struct SegueSender {
    var collectionView:UICollectionView?
    var collectionIndexPath:IndexPath?
    var tableCellIndexPath:IndexPath?
}

Create Instance of Above Struct and send the Object to PerformSegue

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

 let object = SegueSender(collectionView: collectionView, collectionIndexPath: indexPath, tableCellIndexPath: IndexPath(row: 0, section: collectionView.tag))

    self.performSegue(withIdentifier: "showDiscover", sender: object)

}

For prepare for Segue

func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let segueSender = sender as? SegueSender {
let collectionView = segueSender.collectionView as? UICollectionView
let collectionViewIndexPath = segueSender.collectionIndexPath as? IndexPath
let collectionViewTableCellIndexPath = segueSender.tableCellIndexPath as? IndexPath

//// Now you can do whatever you want to collection or table cell
}




}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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