1

This seems so remedial to me, but I am having issues with it. I simply want to transition to another tableviewcontroller when a certain static cell is tapped.

I control-clicked from the cell to the new tableviewcontroller and there was no issue with that -- the segue is created. But when I run the app, nothing happens when I click the cell. Anyone have this same issue pop up?

 override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let storyboard : UIStoryboard = UIStoryboard (name: "groups", bundle: nil)
    let groupsVC = storyboard.instantiateViewController(withIdentifier: "groups") as! GroupsViewController
    if indexPath.section == 1 && indexPath.row == 0 {
    self.navigationController?.pushViewController(groupsVC, animated: true)
    }
ChrisK
  • 57
  • 9

3 Answers3

1

If you have static cells created in a UITableViewController not in UIViewController with UITableView on top of it, you can do the segue to another controller within UIStoryboard.

enter image description here

After that if you use push segue, make sure the UITableViewController is embedded within a UINavigationController.

enter image description here

Aragunz
  • 511
  • 5
  • 18
  • this is what I tried initially and had no luck...with our without the embedded navigation controller. I'm wondering if it doesn't work because this particular tableview controller is not my initial controller? EDIT--I just realized you said to have a tableview on top of the tableview controller. That seems to be less clean...I'm going to try @Sneak 's solution first. Thanks. – ChrisK Jan 29 '17 at 18:38
  • This method above works without code as u requested and i didn't say to use tableview on top of viewcontroller i said exactly the opposite. Please read carefully before jumping to conclusions. – Aragunz Jan 30 '17 at 08:51
  • Thank you for taking the time to write such a lovely reply. Not grasping the instruction is not the same as "jumping to conclusions." "in a UITableViewController...with UITableView on top of it" sounds a lot like "put a tableview on top of a tableviewcontroller," but maybe I need to get more experienced. Again, thank you for your help. – ChrisK Jan 31 '17 at 00:35
0

You need to implement the delegate method in your tableViewController:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
 // Handle your tap to the cell here

    UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    YourViewControllerToPush *vc= [storyboard instantiateViewControllerWithIdentifier:@"YourViewControllerStoryboardIdentifier"];

[self.navigationController pushViewController:vc animated:YES];
// Or to present it [self presentViewController:vc animated:YES completion:nil];

}

EDIT: You can also use the segue identifier method, as described here: However, you need to call the action anyways inside the delegate described above.

[self performSegueWithIdentifier:@"MySegue" sender:sender];

Swift:

Push viewcontroller

didSelectRowAtIndexPath

Community
  • 1
  • 1
  • So this can only be done programmatically? I thought it would be like storyboarding a button used to go to a new view controller. – ChrisK Jan 29 '17 at 05:25
  • Yes. The tableViewCell is not a button. You need to handle the selection inside the delegate method as I wrote. Your Option 1: If you want to push the viewController programmatically, use the first method above. But you need to set a storyBoardIdentifier for the viewController. Press the viewcontroller and in the right side you can set the storyboard identifier. Option 2: Link your viewcontroller with a segue, name the segue with an identifier in the storyboard and use the second option "performSegue...", I prefer the first method much cleaner. Accept the answer if this solves your issue. –  Jan 29 '17 at 06:05
  • @ChrisK You can also add a button to the cell, and add an action to the cell button. But this is as clean, unless you want to customize the button design, then it is perfectly ok. You still need to add the action to the button however. –  Jan 29 '17 at 06:07
  • I prefer the cleaner method as well. I'm having some trouble recognizing parts of your code. I'm using Swift and while the methods are recognizable, some of the characters aren't (ie. @, *). I'll give it a shot anyway and be sure to mark it as answered should I be successful. Thanks. – ChrisK Jan 29 '17 at 06:49
  • @ChrisK @"" is basically a string value. It is basically initializing your Storyboard where "Main" is your storyboard name. And then you initialize the UIViewController you want to push, with instantiateViewController. Thats it. The delegate method for the tableview can be found here : https://developer.apple.com/reference/uikit/uitableviewdelegate/1614877-tableview , make sure you check the indexPath (row or/and section) is correct for the cell that is selected if you have multiple cells, otherwise every cell will trigger the same action. Check updated links in my answer for swift methods. –  Jan 29 '17 at 10:12
  • Still having no luck. I added my code above. I'm getting an error message on the "let groupsVC =..." line for the ("groups") which says, "argument passed to call that takes no argument." If I remove the argument altogether, the app will build and run, but still no movement when I tap the cell. In addition, I don't know where to add in the specific cell (it's section 1, row 0, by the way). – ChrisK Jan 29 '17 at 19:20
  • @ChrisK Check the links for the latest Swift methods i posted in answer. You probably has done something wrong in your initialization in the line. However, you need to set the tableView.delegate = self , in your viewDidLoad, unless you have done it in storyboard already. Otherwise the delegate method didSelectRowAtIndexPath will not get called. just do an IF Statement , where you check that indexPath.section == 1 && indexPath.row == 0, then you fire the action to push the view. –  Jan 29 '17 at 21:35
  • I updated my code above to include the indexPath target. Also added the delegate line...that was missing. Still nothing. When the row is clicked, it's not doing anything at all...no grey highlight or anything. I checked the links you supplied, but I'm still not getting it. One confused me into thinking the this code is supposed to be on the destination controller file? Is that right? Thanks for you patience on this...I'm very frustrated. – ChrisK Jan 29 '17 at 23:08
  • @ChrisK What I am describing needs to be called on the presenting viewcontroller, not the destination one, and it solves your problem in your question here, however, the problem now seems to be that you have not learned the "basics" of a UITableView , how it works and how it uses the delegate methods. This is not a simple task to write in some lines here, you really need to check out some tutorials, youtube are full of good and simple tutorials. It wont take more than 1 hour to grasp the entire concept, the video tutorials usually are around 15-20minutes long. –  Jan 29 '17 at 23:30
  • I've seen tons of them. I'll look at more. Do you have any advice on why the cells aren't behaving as if they have been clicked on? No grey, etc. That seems to be a non-starter in accomplishing what I'm trying to accomplish... – ChrisK Jan 29 '17 at 23:40
  • 1
    @Sneak...I figured it out. I had some code at the top that dismissed the keyboard when pressing outside the textfield in one of the cells. That must have been overriding the selectability of the cells. Thank you for your help. Answered. – ChrisK Jan 29 '17 at 23:49
0

I followed Aragunz's solution which initially didn't work ... I found out a couple hours later that I had a tapGestureRecognizer set on my TableView to allow it to dismiss the keyboard when it is tapped.

This was conflicting with the segue set on the static cell. I commented the tapGesture related code and everything worked fine

//        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(SettingsTableViewController.hideKeyboard))
//        tapGesture.cancelsTouchesInView = true
//        settingsTableView.addGestureRecognizer(tapGesture)

I'll have to find another solution to dismiss my keyboard :D

Edouard Barbier
  • 1,815
  • 2
  • 19
  • 32