如何将值从收集视图传递到表视图
问题描述:
我在集合视图中显示数据下面的数据。如何将值从收集视图传递到表视图
我的JSON数据是这样的:
data {
type : max
id : 1234
type : vete
id : 3445
type : tomoe
id : 2220
type : matye
id : 9087
}
同样喜欢每种类型有像一些数据,例如:
类型:最大 ID:1234
这上述类型有:
[
{
name : max 1
description : masncj
name : max 1
description : masncj
name : max 2
description : masncj
name : max 3
description : masncj
name : max 4
description : masncj
}
]
并且我在表格视图中显示上述数据名称,说明。
现在我需要的是,当我点击任何数据类型名称,如max,vete,tomoe等在集合视图中。
我需要重定向到表格视图作为push segue,并在该表格视图中我需要显示相应的数据。
就像我在集合视图中按下max类型名称单元格 - 那么它必须重定向到表格视图,在那里我需要填充max1,description。
我需要为此使用一个集合视图和一个表视图控制器。
如何传递类型ID以及如何查看各自的数据 - 当我在集合视图中按下任何数据时 - 我需要锁定相应的数据名称,表格视图中的描述?
答
首先使用Storyboard创建一个从CollectionViewVC到TableViewVC的segue,并给它一个类似“collection_to_table”的标识符名称。
两个视图控制器
var typeId = ""
现在CollectionViewVC在点击式创建一个变量名TYPEID,在CollectionViewVC
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if segue.identifier == "collection_to_table"
{
let secondVC : TableViewVC = segue.destinationViewController as! TableViewVC
secondVC.typeId = typeId
}
}
执行此
typeId = <actualTypeId> //This is dynamic value from your json
performSegueWithIdentifier("collection_to_table", sender: self)
现在覆盖的方法
现在你在TableView中有了typeId值你可以用它来获取信息。
希望这对你有所帮助。如果你觉得很难理解,请告诉我。
答
1.
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{
@IBOutlet weak var tblview: UITableView!
var fruit = ["tomato","mango","orange","banana","apple"]
var fruitsimg : NSMutableArray = ["tomato_small.png","images-1.jpg","fruit-1218149_960_720.png","4164249-fruit-images.jpg","5135668-fruit-images.jpg",]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, t/Users/agile-14/Desktop/4164249-fruit-images.jpgypically from a nib.
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 20
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 20
}
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int)
{
view.tintColor = UIColor.red
}
func tableView(_ tableView: UITableView, willDisplayFooterView view: UIView, forSection section: Int) {
view.tintColor = UIColor.yellow
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return fruit[section]
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return fruit.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)as! TableViewCell1
cell.lbl1.text = fruit[indexPath.row]
cell.img1.image = UIImage(named: fruitsimg[indexPath.row] as! String)
cell.img1.layer.cornerRadius = cell.img1.layer.frame.height/2
cell.img1.clipsToBounds = true
cell.img1.layer.borderWidth = 2
cell.img1.layer.borderColor = UIColor.black.cgColor
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
var vc1 = storyboard?.instantiateViewController(withIdentifier: "collViewController") as! collViewController
vc1.arr = fruitsimg
//vc1.str = fruitsimg[indexPath.row]
self.navigationController?.pushViewController(vc1, animated: true)
}
2.
class collViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource{
@IBOutlet var coll: UICollectionView!
var arr : NSMutableArray = []
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return arr.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = coll.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)as! CollectionViewCell1
cell.img2.image = UIImage(named: arr[indexPath.row] as! String)
return cell
}
感谢您solution.But居然在我的收藏view.swift在我tableviewcontroller.swift我必须分配'VAR TYPEID = “” '然后在我的集合视图vc ..on type Id =的意思是..我必须动态显示。因为我现在有一些5数据和5 typ类型的id.So可能是5数据可以chnage到50未来的知道。那么我如何分配类型,如果价值? –
user5513630
而且..我需要显示名称,描述基于我的单元格在集合视图中的点击操作...所以通过这个执行seague ....我也可以显示名称,描述.....当我点击任何单元名称? – user5513630
在集合视图上单击您将具有视图的位置。从那个位置你可以分配一个id给那个typeId变量。你必须有一个类型的列表? 。你能告诉我你如何将数据提供给收藏视图? – imagngames