2

I have created a screen containing two table views where i have successfully called the method cellForRowAtIndexPath but now am facing trouble in navigating to the other page from tableview option by using didSelectRowAtIndexPath.I think am doing some mistake and i have clearly no idea what is the next step.Can anyone guide how should i do it?? I have used this code-

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([tableView isEqual:leftTableView]) {

            TattooSinglesScreen *obj=[self.storyboard instantiateViewControllerWithIdentifier:@"TattooSinglesScreen"];

            EyecatcherScreen *obj1=[self.storyboard instantiateViewControllerWithIdentifier:@"EyecatcherScreen"];

            TattooToplist *obj2=[self.storyboard instantiateViewControllerWithIdentifier:@"TattooToplist"];

            int i=indexPath.row;
            if(i==0){
                [self.navigationController pushViewController:obj animated:nil];
            }
            else if (i==1) {
                [self.navigationController pushViewController:obj1 animated:NO];
            }
            else if (i==2) {
                [self.navigationController pushViewController:obj2 animated:NO];
            }
    }
    else {
        TattooSinglesScreen *obj=[self.storyboard instantiateViewControllerWithIdentifier:@"TattooSinglesScreen"];

        EyecatcherScreen *obj1=[self.storyboard instantiateViewControllerWithIdentifier:@"EyecatcherScreen"];

        TattooToplist *obj2=[self.storyboard instantiateViewControllerWithIdentifier:@"TattooToplist"];

        int i=indexPath.row;
        if(i==0){
            [self.navigationController pushViewController:obj animated:nil];
        }
        else if (i==1) {
            [self.navigationController pushViewController:obj1 animated:NO];
        }
        else if (i==2) {
            [self.navigationController pushViewController:obj2 animated:NO];
        }
   }
}
Bhavin Ramani
  • 3,221
  • 5
  • 30
  • 41
pri
  • 87
  • 11
  • What problem do you face?Exactly tell me the trouble. – user3182143 Jul 14 '16 at 12:46
  • You done same code in else part of the didSelectRowAtIndexpPath method – user3182143 Jul 14 '16 at 12:47
  • By using this code am trying to navigate to the other page in both the table view but its not working. Nothing is happening – pri Jul 14 '16 at 12:48
  • ya i am just checking if its working or not...later on i will use other pages – pri Jul 14 '16 at 12:49
  • still not working :( – pri Jul 14 '16 at 12:50
  • Check my below answer – user3182143 Jul 14 '16 at 12:55
  • Have you got solution? – user3182143 Jul 14 '16 at 13:08
  • This code has some mistakes, but it's not sufficient to show us what the problem is. A bunch of things could be the reason why the next view controller isn't being properly pushed onto the navigation controller's stack. You _do_ have a navigation controller, right? Also, the fact that you manually instantiate view controllers instead of designing your storyboard flow accordingly is kind of unusual, given that you seem to only have three different rows in the table (i.e. why use a table in the first place and not rely on simpler views and segues?) – Gero Jul 14 '16 at 13:13
  • Actually i have to use dynamic values and i had no idea if thats gonna work on View. I thought it would be better to use tableview instead of a view for adding and removing values. – pri Jul 14 '16 at 13:23
  • pri see my updated answer – user3182143 Jul 14 '16 at 13:27
  • If my answer is not helpful for you,I will delete my answer. – user3182143 Jul 14 '16 at 13:28
  • @user3182143 ok sir am gonna try. No need to delete. It might help some other person. – pri Jul 14 '16 at 13:29
  • I'm feeling really bad now, user3182143 is clearly meaning well. I hope the answer will at least point you in the right direction. I have not enough time atm, but I'll try to post a little more elaborate solution of how I would to this tonight. – Gero Jul 14 '16 at 13:30
  • thanks for helping me out @user3182143 & Gero – pri Jul 14 '16 at 13:33

2 Answers2

0

If you want to navigate from table view to another view controller don't set animated:NO.set animated:YES for navigating.But if you set animated:NO,definitely it does not navigate.So you have to do following things for navigating.

YourViewController *yourVC = (YourViewController *)[self.storyboard instantiateViewControllerWithIdentifier:@"StoryBordIdentifier"];
[self.navigationController pushViewController:yourVC animated:YES];

Also You can use in this didSelectRowAtIndexPath

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

with

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
  YourViewController *vc = segue.destinationViewController;
}

Click Table Row and Go another Page

Segue or didSelectRowAtIndexPath

Making Push Segue when table row is selected

Community
  • 1
  • 1
user3182143
  • 9,459
  • 3
  • 32
  • 39
  • This is not true, whether the transition is animated or not has no importance on whether the next view controller is pushed or not. Giving `nil` instead of `YES` or `NO` is incorrect, though. – Gero Jul 14 '16 at 13:08
  • If animated NO and nil means how can it navigate to other view? – user3182143 Jul 14 '16 at 13:10
  • See his above coding.Everything is no and little nil for animated. – user3182143 Jul 14 '16 at 13:12
  • Yes, I see that. Assuming `nil` is meant to be `NO`, that just means the transition is not supposed to be animated. The view controller will simply appear immediately (and not slide in from the right). That's what the parameter means. What else should it mean? What good would it be to call a method and then give it a parameter that means "don't do anything"? You could as well just not call it. I'm sorry, but I downvoted your answer because it is simply wrong and we shouldn't promote incorrect answers on SO. – Gero Jul 14 '16 at 13:15
  • I'm sorry, I don't want to bash on you, but "But if you set animated:NO,definitely it does not navigate" is simply wrong. As in: The opposite is true. It navigates, it just doesn't animate. Furthermore, your additions will not help, because they would require that the storyboard has a segue connected to the fitting view controller type. Since pri instantiates said view controllers and then manually pushes to them I assume that's not the case. It could work better, but requires more setup, definitely. The prepareForSegue code you posted does basically nothing, unfortunately. – Gero Jul 14 '16 at 13:27
  • I am stunned what made you think I hadn't immediately tried it, since it was so out of place... Anyways, your edit seems to have helped pri, so yay. I removed the misleading part of your answer, hope that's okay with you. I also will add a repo showing the issue below, in a separate answer, for completeness. – Gero Jul 14 '16 at 16:19
0

I see user3182143's answer helped, but for what it's worth I've set up a tiny demo project (now updated with two tables) to show anybody interested how it's done.

It also illustrates (as the name implies), that you can, indeed, push manually and set the animation parameter to NO, just run it in the simulator.

I can't tell why that didn't work in your original approach, pri, but my guess is that you set up your navigation view controller wrongly somehow.

For code & storyboard readability I'd recommend using segues anyways.

Gero
  • 4,394
  • 20
  • 36
  • Sir your demo shows usage of only one table view but i am having problem with TWO TABLE VIEWS in a same View Controller . I am not facing any sort of problem with a single table view. My case is different. – pri Jul 15 '16 at 04:13
  • PRI don't confuse with Gero answer. – user3182143 Jul 15 '16 at 04:21
  • If you have problem still,I will help you. – user3182143 Jul 15 '16 at 04:23
  • ya am still facing problem sir..coz am using two table views and not one in a same view controller...so i have to use each method for both of them @user3182143 – pri Jul 15 '16 at 04:31
  • As was mentioned before, you do exactly the same for the second table in your code. Also, handling two tables isn't per se affecting issues with presenting another view controller once either table's rows are clicked. Oh, and @user3182143: Seriously? __I__ am the one confusing people when you don't even accept a correction to a wrong answer? That's petty, but okay, to each their own I guess. – Gero Jul 15 '16 at 16:18
  • Sorry PRI all of sudden I am at out of station I couldn't help you.I will help you on Monday. – user3182143 Jul 16 '16 at 01:41
  • Gero I an not saying that your answer is wrong.He commented that in your project you used only one table view.If you give demo with two table view with condition probably he understand.I gave the solution with his related mistakes. – user3182143 Jul 16 '16 at 02:01
  • Even if you look over other answers related to this question most answer replied above my answer that is the very basic thing.Even I tried sample for this question l got the solution.It is navigating successfully.Gero I agree with correct answer if the answer is better and good.We are here to help OP.In some case even we give the wrong idea,answer and advice.We should realize what we do here.If it is not accepted we try to improve and give the unique answer.we must not do the same thing.Let help people without any grievance here. – user3182143 Jul 16 '16 at 02:02
  • We learn each other new concept every day and help who are all facing problem. – user3182143 Jul 16 '16 at 02:04
  • Sorry, user3182143, maybe it's a communication problem, but what you said is exactly the point I'm making. Like you, I also tried to help, pri _and_ you. I would not have posted a second answer if yours was not incorrect regarding the animation. You seemed to take my downvote personally (though are especially meant for incorrect answers), even advising pri to not take my answer into account (pri's problem seemed not only related to the number of tables). You did not accept the correction edit I proposed, which would have made _your own_ answer better. I guess that's it for me here, then. Bye. – Gero Jul 16 '16 at 15:11