当我在Tableview中嵌入多个Collection视图时,如何进行segue?

时间:2017-12-13 15:37:01

标签: ios swift uicollectionview

我在准备(for segue)时遇到了访问collectionview.tag的问题。我尝试了不同的东西,但没有任何工作。

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?) {


    }
}

2 个答案:

答案 0 :(得分:0)

您可以将self.performSegue中的collectionview对象传递为:

 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)

}

然后在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
       }



}

这将有效

答案 1 :(得分:0)

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
}




}