1

I'm creating an app with three table views (each in a different VC). I added everything to the main.storyboard, but I have to make one cell custom in a XIB file with three Labels. So my question is how can I add a segue from the cell in the XIB file to the ViewController in the Storyboard.

I saw that here's an answer but I don't understand this : Segue from Storyboard to XIB

ndmeiri
  • 4,979
  • 12
  • 37
  • 45
Just4School
  • 117
  • 1
  • 1
  • 9
  • What do you mean by how to segue from cell to view controller? Do you want a solution where tapping on a cell will take you to next view controller and also pass the data of that specific table view cell? – Sanket Ray Jul 03 '18 at 18:52
  • Only a `UIViewController` object can do a segue. Your cell needs to tell its parent (UITableView) and its parents needs to tell the `UIViewController` to perform the segue. You can use block, delegates, etc. to do so. – Larme Jul 03 '18 at 19:26
  • Why did you use an xib for the cell? Why not use a prototype cell in your storyboard scene? – Paulw11 Jul 03 '18 at 20:27
  • @Sanket Ray Yes, exactly. I had the conplete solution once, but then I saw, that I have to add three labels so I had to this this in an XIB file where I cannot easily ctrl+click+drag to the next tableView. – Just4School Jul 04 '18 at 06:30
  • @Paulw11 Can I add there multiple labels? I tried it once but somehow after that nothing worked anymore. – Just4School Jul 04 '18 at 06:32
  • @Larme I have the segue, but I cant segue from a specifc cell in the 2nd tableView to the tableView which belongs to it. – Just4School Jul 04 '18 at 06:35
  • Of course. You can add whatever you want to the cell and add constraints so that it is laid out correctly – Paulw11 Jul 04 '18 at 06:37
  • @Paulw11 I tried that and added it to my VC file but that didnt worked and Im getting an error now, that the label outlet from the ViewController to the UiLabel is invalid. Outlets cannot be connected to repeating content. How can fix this? – Just4School Jul 04 '18 at 09:07
  • You connect labels to outlets in your `UITableViewCell` subclass – Paulw11 Jul 04 '18 at 09:43

2 Answers2

2

If you know the position of your customCell then you can easily achieve this programmatically by calling tableView delegate following ways: Suppose your custom cell is in the last index then:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    guard indexPath.row == models.count - 1 else {
        //behavior of other cell
        return
    }
    navigateToTestViewController()
}

But if you have a button in a customCell then you have to use delegate or reactive approach to send delegate to parentController to perform navigation.

Update: If you only need to make cell of type customCell navigable then you can do following way:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let cell = tableView.cellForRow(at: indexPath)
    guard cell is CustomCellTableViewCell else { return }
    navigateToTestViewController()
}
prex
  • 719
  • 4
  • 17
  • The position is always on another place because the user can input cells by himself – Just4School Jul 04 '18 at 06:27
  • So how do you know when to dequeue for a customCell? – prex Jul 04 '18 at 06:33
  • I dont know :D Im knew in coding so I make a lot of mistakes – Just4School Jul 04 '18 at 15:04
  • So what do you mean by “user can input cells by himself”? Is cell added dynamically during runtime? Can you explain your use-case or share your code extract? – prex Jul 04 '18 at 15:08
  • My app should manage bookkeeping of a club. First Tableview: club then segue to the members of the club and from there to the transactions of the Member. With new cell i mean for example a new member which creates a new cell for this member – Just4School Jul 04 '18 at 15:40
0

This question was answered quite nicely previously:

https://stackoverflow.com/a/44675813/4686915

here's the meat of it:

let myViewController = MyViewController(nibName: "MyViewController", bundle: nil)
self.present(myViewController, animated: true, completion: nil)

or push in navigation controller

self.navigationController?.pushViewController(MyViewController(nibName: "MyViewController", bundle: nil), animated: true)

The only real difference is that you have to make sure that you're making the self... calls within your UIViewController.

MQLN
  • 2,292
  • 2
  • 18
  • 33