Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 917
user3163404 Avatar asked Mar 17 '26 03:03

user3163404


1 Answers

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.

like image 158
Samhan Salahuddin Avatar answered Mar 21 '26 08:03

Samhan Salahuddin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!