从多个UICollectionViews中的电影列表中显示详细视图

时间:2018-04-30 18:58:12

标签: ios swift uiviewcontroller uicollectionview segue

我在同一个ViewController中有多个UICollectionViews(准确地说是4个)。我为每一行'做了一个容器视图。它由Title和UICollectionView组成。我从每个UICollectionViewCell到详细信息视图控制器发出了一个Segue,并为它们提供了一个标识符。但我无法理解如何将我的Movie对象从containerview转到详细信息页面。

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if (segue.identifier == "MovieDetailsPlaying"){
        var destination = segue.destination as! DetailMovieController
        destination.item = containerNowPlayingView.movies[IndexPath]
    }
}

所有我认为我需要解决这个问题的方法是弄清楚如何在ViewController中的Segue的prepare函数中获取IndexPath。我希望你能帮助我。

我已经找到了这样的答案: Answer one

My Segue's

2 of the 4 horizontal UICollectionViews

1 个答案:

答案 0 :(得分:1)

考虑到你没有显示你在cellForItemAt indexPath内使用的细胞类型,我不得不做些什么,这样你就可以清楚了。我也不知道你的意思是你有4个集合视图,但这是通过prepareForSegue使用1 collectionView将数据从collectionViewCell传输到目标视图控制器的方法之一。

preparForSegue内你需要3件事:

1-您需要知道您在cellForItemAt indexPath中使用的单元格类型。在本例中,我使用了一个名为MovieCell

的单元格类

2-您需要获取所选行的indexPath。 CollectionView上有一个名为indexPath(for :)的方法,它接受一个collectionViewCell作为争论:collectionView.indexPath(for: UICollectionViewCell)

3-从与所选行对应的数组中获取信息。

例如,您有一个名为MovieDataModel的类,其中包含movieName属性:

class MovieDataModel{
    var movieName: String?
}

collectionView单元名为MovieCell,其中有一个movieTitleLabel插座:

class DetailedSearchComplaintCollectionCell: UICollectionViewCell {
    @IBOutlet weak var movieTitleLabel: UILabel!
}

使用CollectionView在类中:

@IBOutlet weak var collectionView: UICollectionView! // in this example there is only 1 collectionView
let movies = [MovieDataModel]() // an array of MovieDataModels

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return movies.count // return the number of items inside the movies array
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "movieCell", for: indexPath) as! MovieCell // cast the cell as a MovieCell
     cell.movieTitleLabel.text = movies[indexPath.row].movieName! // this is the same info we need for the 3rd step

     return cell
}

                                                 // sender parameter <<<
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if (segue.identifier == "MovieDetailsPlaying"){

       // From the steps above
       let cell = sender as! MovieCell // 1- the same exact type of cell used in cellForItemAt indexPath. Access it using the sender parameter in the prepareFoeSegue signature and then cast it as a MovieCell
       let indexPath = collectionView.indexPath(for: cell) // 2- get the indexPath from the row that was tapped. Add the cell from step 1 as an argument to it. Notice it says collectionView because that's the name of the collectionView outlet. I'm not sure how this would work with 4 different collectionViews but you probably will have to make an indexPath for each one. Just an assumption

       var destination = segue.destination as! DetailMovieController
       destination.item = movies[indexPath!.row].movieName // 3- what MoviesDataModel item from inside the movies array and the title from it corresponds to the indexPath (the tapped row).
    }
}

我不知道你使用的collectionView单元格的名称,但是在带有collectionView的类中,只要你看到名称,MovieCell只需将其更改为你使用的单元格的名称。