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

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

问题描述

我有5个收集视图单元,名称为Resturants,商店,医疗,客厅,公园等。所以现在我设置了10个视图控制器,我将表视图放在每个五个控制器中,我调用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

比如说,如果用户按下,餐厅,我将重定向到一个视图controlellr.In我已经调用了`baseurl / category_01的webservies。所以从这个网址我将获取名称,地址,图像,我将在我的表视图中显示

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.

任何人都可以建议我使用单视图controlellr和一个表格视图来查看我的所有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个单元格数据,我将在显示表视图。所以当我使用一个视图controllelr和一个表视图时,我需要提一下,`哪个indespath.row按,这应该等于我的类别id,shoyld显示相关数据。

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]

然后,当要转换到另一个视图时,检查已选择的类别,并使用相关数据填充tableview。您可以设置如下结构:

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天全站免登陆