Reputation: 1699
I am displaying below data in collection view.
And my JSON data is like:
data {
type : max
id : 1234
type : vete
id : 3445
type : tomoe
id : 2220
type : matye
id : 9087
}
Same like each type have some data like for example:
Type: max id: 1234
This above type have:
[
{
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
}
]
And I am displaying the above data name, description in table view.
Now what I need is, when ever I click any data type name like max, vete, tomoe, etc. In collection view.
I need to redirect to table view as push segue and in that table view I need to display the respective data.
Like if I press max type name cell in collection view - then it have to redirect to table view and there I need to populate max1, description.
I need to use one collection view and one table view controller for this.
How can I pass the type id and how can I see the respective data - when I press any data in collection view - I need to shoe that respective data names, description in table view?
Upvotes: 0
Views: 834
Reputation: 270
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
}
Upvotes: 0
Reputation: 350
Firstly create a segue from CollectionViewVC to TableViewVC using Storyboard and give it a identifier name something like "collection_to_table".
Create a variable name typeId in both the View controller as
var typeId = ""
Now in CollectionViewVC On click on type , perform this
typeId = <actualTypeId> //This is dynamic value from your json
performSegueWithIdentifier("collection_to_table", sender: self)
Now override a method in CollectionViewVC
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if segue.identifier == "collection_to_table"
{
let secondVC : TableViewVC = segue.destinationViewController as! TableViewVC
secondVC.typeId = typeId
}
}
Now You have typeId value in TableView Which you can use to fetch info.
Hope this help you . Let me know if you find it hard to understand .
Upvotes: 1