【问题标题】:Passing an array of images from tableview to collectionview将一组图像从 tableview 传递到 collectionview
【发布时间】:2018-07-21 23:16:14
【问题描述】:

我必须查看控制器。第一个是表格视图,第二个是第二个详细视图中的集合视图。我创建了一组图像数组。我希望单击单元格表并将一组图像传递给集合视图。我用数组数组定义了一个文件模型。我还为单元格表和单元格集合视图定义了另外两个控制器。

问题:我无法传递这个数组图像数组。我确实使用 prepare for segue 将字符串数组传递给了详细信息中的 textview。沃尔克在那里,我将非常感谢您的帮助。可以看一下GitHub中的项目:https://github.com/ricardovaldes/hotelesWithIos9

此刻,当我点击表格单元格时,我会得到一些不同数组的图片,无论我点击哪个单元格,总是相同的图片。

import UIKit

class DetailVCViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

    let hotel = Hotel()
    var myDescription = ""

    @IBOutlet weak var myCollection: UICollectionView!

    @IBOutlet weak var label: UILabel!

    @IBOutlet weak var hotelDescription: UITextView!

    override func viewDidLoad() {
        super.viewDidLoad()

        hotelDescription.text = myDescription
    }


    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return hotel.array.count
    }

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

        let cell = myCollection.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath) as! CollectionCellCollectionViewCell

        cell.cellImage.image = UIImage(named: hotel.array[indexPath.row][indexPath.row])

        return cell
    }

    @IBAction func reservar(_ sender: UIButton) {

    }


}


import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet weak var myTable: UITableView!

    let hotel = Hotel()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        let cell = UINib(nibName: "TableViewCell", bundle: nil)
        myTable.register(cell, forCellReuseIdentifier: "tableCell")

    }

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = myTable.dequeueReusableCell(withIdentifier: "tableCell", for: indexPath) as! TableViewCell

        cell.hotelName.text = hotel.hotelNames[indexPath.row]
        cell.hotelImage.image = UIImage(named: hotel.hotelImages[indexPath.row])
        cell.hotelImage.contentMode = .scaleToFill
        return cell

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

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        performSegue(withIdentifier: "mySegue", sender: self)
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let index = myTable.indexPathForSelectedRow
        let indexNumber = index?.row
        let secondVC = segue.destination as! DetailVCViewController
        secondVC.myDescription = hotel.description[indexNumber!]

    }

}

【问题讨论】:

  • 表格中的一些代码和细节:

标签: arrays swift uicollectionview tableview


【解决方案1】:

DetailVCViewController 中,您正在创建Hotel 的新对象,因此该对象与您在TableView 中选择的对象不同cellViewController 中。将hotel 对象替换为array of String,如下所示,以便您可以将所选TabelView 单元格索引中的数组传递给DetailVCViewController

class DetailVCViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

    var array: [String] = []
    var myDescription = ""
    ....
} 

现在在ViewController 中,更新prepare(for segue: 如下以传递数组

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

   ....

   override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
      let index = myTable.indexPathForSelectedRow
      let indexNumber = index?.row
      let secondVC = segue.destination as! DetailVCViewController 
      secondVC.myDescription = hotel.description[indexNumber!]
      secondVC.array = hotel.array[indexNumber!]

   }
}

【讨论】:

  • 效果很好。现在我知道承认我的错误了。我试图获得一个数组数组,而不是要显示的简单数组。非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-14
  • 2013-03-03
  • 2017-07-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多