首页 > 解决方案 > 将信息从集合视图传递到表视图

问题描述

现在我有一个表格视图,它是集合视图的扩展。

这是故事板的图片。 在此处输入图像描述

该应用程序的主要前提类似于Apple Maps。我希望选择一个 collectionView 单元格,然后让 viewController 显示一个 tableView,其中包含该类别中所有受尊重的项目。

这是我的 collectionView 代码。

 class ContentViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

let imageArray = [UIImage(named: "image 1"), UIImage(named: "image 2"), UIImage(named: "image 3")]

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

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CustomCell", for: indexPath) as! CustomCell
    cell.mapIconImage.image = imageArray[indexPath.row]
    cell.mapIconLabel.text! = imageNameArray[indexPath.row]
    return cell
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    performSegue(withIdentifier: "ShowTableViewPlaces", sender: self)
}

class CustomCell: UICollectionViewCell {

@IBOutlet weak var mapIconImage: UIImageView!
@IBOutlet weak var mapIconLabel: UILabel!

}

这是到目前为止 tableView 的代码。

import UIKit

struct PlacesInTableView {
var name: String

init(name: String) {
    self.name = name
}
}

class MapItemsTableViewController: UITableViewController {

var image1InTableView = [PlacesInTableView(name: "place 1"),
    PlacesInTableView(name: "place 2"),
    PlacesInTableView(name: "place 3")
]

var image2InTableView = [PlacesInTableView(name: "place 1")
]

var image3InTableView = [PlacesInTableView(name: "place 1"),
    PlacesInTableView(name: "place 2")
]

选择集合视图时,我希望该受尊重类别中的所有位置都占据 tableview 的内容。我将如何将数据从集合视图单元格传递到新的 tableView?

标签: iosswiftxcodeuitableviewuicollectionview

解决方案


据我了解,您想将name价值传递给MapItemsTableViewController?

您可以覆盖准备功能:

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

    let viewController = segue.destination as! MapItemsTableViewController

    viewController.name = // Data can input here

}

推荐阅读