ios - 当 Tableview 中嵌入了多个 Collection View 时,如何进行 Segue?

标签 ios swift uicollectionview

在进行准备(用于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?) {


    }
}

最佳答案

您可以在 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
       }



}

这会起作用

关于ios - 当 Tableview 中嵌入了多个 Collection View 时,如何进行 Segue?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47796894/

相关文章:

ios - 在 iOS 导航 Controller 中全局翻译 'back' 文本

swift - 当我在 Swift 中保存到 NSUserDefaults 时,NSDictionary 变成 NSCFString

ios - 更新未选中的 CollectionView 单元格?

iOS UICollectionview 渲染背景颜色错误

ios - UITextView 两次绘制文本

ios - 从 SceneDelegate 更新屏幕结构的状态

ios - Swift Firebase 加载图片的次数太多

xcode - 从 UICollectionViewCell 呈现一个 UIPopoverController

ios - Facebook iOS SDK 强制登录

ios - 在 viewDidLayoutSubviews 中找不到约束 - AutoLayout