现在,我有一个表视图,它是集合视图的扩展。
该应用程序的主要前提类似于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?
据我了解,您想将name
值传递给MapItemsTableViewController
吗?
您可以覆盖准备功能:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let viewController = segue.destination as! MapItemsTableViewController
viewController.name = // Data can input here
}