use single table view controller for each collecti

2020-05-05 18:25发布

问题:

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

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

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.

This is my first app in swift 2.0.

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

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
},
]

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.

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")
    }

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

Thanks

回答1:

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")
.
.
.
}