user3163404
user3163404

Reputation: 359

TableView Cell click to another TableView

I have a tableView on my App. I want to click on a cell and go to another tableView. For example a list of Artists and then list of albums.

Normally I just drag the "Prototype Cells" to the other view controller. But nothing happens.

Here is my code:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return NumberOfArtists
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")

    print(NameOfArtist)

    cell.textLabel?.text = NameOfArtist[indexPath.row]
    cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator

    return cell

}

Here is my Storyboard:

UPDATE: I've removed the segue from the TableView (the one that is on the back). Still not working.

enter image description here

Steps

1 - Create a tableView and add source and delegate. 2 - Create another View Controller and add a tableView. 3 - Create a segue from the first prototype cell to the second tableView.

Nothing happens.

Upvotes: 3

Views: 708

Answers (3)

john Smith
john Smith

Reputation: 56

I think you don't need two viewController, as per your design you can use First tableView and Cell for both Artist & Album Controller.

You just need Two DataSource 1 for artist and other for Album and one boolean that manage Current Visible DataSource.than using tableView Delegate Methods you can manage Both Artist And Album View..

ex: suppose our boolean Name is isArtist and isArtist = true for Artist View and isArtist = false for Album View.

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
     return isartist ? ArtistDataSource.Count : AlbumDataSource.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")

 if isArtist {
    cell.textLabel?.text = ArtistDataSource[indexPath.row]
 }
else {
    cell.textLabel?.text = AlbumDataSource[indexPath.row]
 }


    cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator

    return cell

}


func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

          if isArtist {
            isartist.toggle()
            yourtableView.reloadData
          }
          else {
       //Do what you wish for Album tableView Selection
      }
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return isartist ? 70 : 100
    }

Upvotes: 2

Samhan Salahuddin
Samhan Salahuddin

Reputation: 2170

I checked out your code you posted to Dropbox. Here's what's wrong with your code :

  1. You need to embed your Main view controller in a navigation controller. You can do this by clicking on your Main view controller in the storyboard and then clicking the Editor-> Embed in -> Navigation controller option.

  2. The cell you create in your cellForRowAtIndexPath method is not the same as the cell you attached the segue to in your storyboard. You create the cell like so :

     let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
    

This actually creates an entirely new cell with the reuse identifier "Cell". It doesn't have the segue attached to it. To do that :

let cell : UITableViewCell! = tableView.dequeueReusableCellWithIdentifier("Cell");

This will get the cell in your storyboard. Hope this helps.

Upvotes: 2

Antoine
Antoine

Reputation: 1149

Edited 8/13/15

  1. You have to create a UITableView as you mentioned for example for the artists.
  2. You create it you want another UITableView for the albums' artists.
  3. You just have to link the segue from the prototype cell or from all the static cells to the UITableView and it should work.

Note : This is how to manage your storyboard. I have not included how to populate the data and to manage the UITableView

Upvotes: 2

Related Questions