如何将值从集合视图传递到表视图

时间:2016-04-21 11:04:35

标签: ios swift uitableview

我在集合视图中显示以下数据。

我的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 type name cell一样 - 然后它必须重定向到表视图,我需要填充max1,description。

我需要使用一个集合视图和一个表视图控制器。

如何传递类型ID以及如何查看相应的数据 - 当我在集合视图中按任何数据时 - 我需要在表格视图中提供相应的数据名称,描述?

2 个答案:

答案 0 :(得分:1)

首先使用Storyboard从CollectionViewVC到TableViewVC创建一个segue,并为其指定一个类似“collection_to_table”的标识符名称。

在View控制器中创建一个变量名称typeId

var typeId = ""

现在在CollectionViewVC中点击类型,执行此

typeId = <actualTypeId> //This is dynamic value from your json   
performSegueWithIdentifier("collection_to_table", sender: self)

现在覆盖CollectionViewVC中的方法

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
         if segue.identifier == "collection_to_table"
        {
            let secondVC : TableViewVC = segue.destinationViewController as! TableViewVC
            secondVC.typeId = typeId
        }
    }

现在,TableView中有typeId值,您可以使用它来获取信息。

希望这对你有所帮助。如果您觉得难以理解,请告诉我。

答案 1 :(得分:0)

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

    }
相关问题