为每个集合视图单元格按使用单个表视图控制器 [英] use single table view controller for each collection view cell press

查看:18
本文介绍了为每个集合视图单元格按使用单个表视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 5 个集合视图单元,名称分别为Resturants"、shops"、Medical"、parlor"、parks"等.所以现在我设置了 10 个视图控制器,我在每个 5 个控制器中放置了表格视图,我正在调用 json 数据并在表格视图中显示.我还有一个自定义单元格,用于所有 iew controlelr 表格视图

i have 5 collection view cell with name "Resturants", "shops", "Medical" , "parlor" , "parks" so on. So now i set like, 10 view controller and i place table view inside each five controller and i am calling the json data and display in table view. I also have one custom cell ,where i am using for all iew controlelr table view

就像说,如果用户按下 Restaurant,我将重定向到一个视图 controlellr.我已经调用了 `baseurl/category_01 的 webservies.因此,我将从这个 url 获取名称、地址、图像,并将显示在我的表格视图中

Like say, if user press, Restaurant, i wil redirect to one view controlellr.In that i have called the webservies of `baseurl/category_01. So from this url i will fetch the name, address, image and i will display in my table view

但我知道这是错误的做法.如果我有 20 个集合视图单元格.那么如果我创建 20 个视图控制器和 20 个表视图意味着这将不好.

But i know this is wrong way of doing.If i have 20 collection view cell.Then if i create 20 view controller with 20 table view means that will be not nice.

这是我在 swift 2.0 中的第一个应用程序.

This is my first app in swift 2.0.

任何人都可以建议我对我的所有 10 个集合视图数据单元格使用带有一个表视图的单视图控制器吗?

Can any one suggest me to use single view controlellr with one table view for all my 10 cell of collection view data.

但是我的集合视图单元格数据中的一件事是我从一个 json url 获取.因为每个数据都被指定为一个类别 id.Like:

But one things in my collection view cell data are i am getting from one json url.In that each data is specified as one category id.Like :

[
{
category_id : 0
category_name : Restaurant
},
{
category_id : 1
category_name : park
},
{
category_id : 2
category_name : shops
},
{
category_id : 3
category_name : medical
},
]

像明智的 10 个单元格数据一样,我将在表格视图中显示.因此,当我将一个视图控制器与一个表格视图一起使用时,我需要提及,`indespath.row 按下并且应该等于我的类别 ID 并且应该显示相关数据.

like wise 10 cell data which i will dispaly in table view.So when i use one view controllelr with one table view i need to mention, `which indespath.row press and that should equal to my category id and shoyld show the relavent data.

现在我在 collectionviewcontroller.swift 中使用的是.我只是按下了哪个索引路径行,我只是导航到另一个视图控制器

Now what i am using in my collectionviewcontroller.swift is .I am just which index path row is press and i am just navigating to that another view controller

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        // You can use indexPath to get "cell number x", or get the cell like:
    let cell  = collectionView.cellForItemAtIndexPath(indexPath)

    if((indexPath.row) == 0) {

        let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let vc: ViewController2 = storyboard.instantiateViewControllerWithIdentifier("ViewController2") as! ViewController2
        self.presentViewController(vc, animated: true, completion: nil)
        print("Layout 1 clicked")

    }
    else if((indexPath.row) == 1) {
        let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let vc: ViewController3Bank = storyboard.instantiateViewControllerWithIdentifier("ViewController3Bank") as! ViewController3Bank
        self.presentViewController(vc, animated: true, completion: nil)
        print("Layout 2 clicked")
    }

那么如何使用单个 viewcontrolel1r 表视图来做到这一点..请帮帮我..

So how to do that with single viewcontrolel1r table view..Please help me out..

谢谢

推荐答案

将类别名称存储在数组或其他位置,每当单元格被推送时,您可以通过从数组中选择某个类别来调用某个类别,如下所示

Store category names in an array or elsewhere, anytime cell is pushed you call the certain category by selecting it from the array as follows

categoryArray[indexPath.row]

然后,当转换到另一个视图即将完成时,您检查已选择的类别并使用相关数据填充表格视图.您可以像这样设置结构:

Then when the transition to another view is about to be done you check what category has been selected and you populate the tableview with relevant data. You can set up a structure like so:

func determineDataSource()
{
  switch category
  {
    case "Restaurant":
      print("restaurant")
.
.
.
}

这篇关于为每个集合视图单元格按使用单个表视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆