0

I have a UITableviewController and when a row is selected, I want to pop back to my rootViewController and have it display the text of the row in table view. What is happening right now is the viewWillAppear in my regular view controller is being called before the popToRootViewController function is called, so when it goes to the regular view controller it does not display the text. I have tried using viewDidAppear also and it doesn't work. Here is the code I have tried:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
    NSLog(@"row was selected");
    user[@"school"] = [[_objects objectAtIndex:indexPath.row] objectForKey:@"title"];
    [[PFUser currentUser] saveInBackground];
    [self.navigationController popToRootViewControllerAnimated:YES];
}

And then in the regular view controller:

- (void)viewWillAppear:(BOOL)animated
{
    NSLog(@"will appear ran");
    PFUser *currentUser = [PFUser currentUser];
    self.userSchoolLabel.text = currentUser[@"school"];
}

What I see in the console is it prints out

"will appear ran"

and then "row was selected"

How can I fix this problem, also if i call the method popToRootViewController from the touch of a button on another view controller it works fine, the problem is with the selecting a row, I not sure why?

Thanks for your help in advance. I really appreciate it.

ChintaN -Maddy- Ramani
  • 5,156
  • 1
  • 27
  • 48
  • Is it a stroyboard based app? – n00bProgrammer May 22 '14 at 04:48
  • Your requirement: select a row -> popToRootView (RegularViewController). But, viewWillAppear of RegularViewController happens first, didSelectRow happens after? Right? – nmh May 22 '14 at 04:48
  • I don't think it's anything in the code you posted. Maybe something in the - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath method? – Fabio May 22 '14 at 05:09
  • Do you have any segues attached to the cell? – rdelmar May 22 '14 at 05:18
  • Yes this is storyboard based app, and I've also tried viewDidAppear but that gets called first which it shouldn't. And no I have no segues attached to the cell –  May 22 '14 at 20:17

1 Answers1

0

Event handling is a part of the Delegate methods. Your datasource seems to be correct as it is showing the row for you. So, may be you have not properly set the delegate of your tableview correctly. If you are using the nib then connect the delegate of your tableview or if you have use the code to draw the tableview simply check for the _yourtable.delegate =self; is set or not.

Another reason could be if there is uiview in the tableview cell then it can take your rowselection event away.

Also, if you are setting editing enabled in the tableview, it may not call the didSelectRowAtIndex: method... for detail see here

Community
  • 1
  • 1
Bikram Thapa
  • 1,329
  • 1
  • 16
  • 29
  • I an not using nib and the delegate is connected to the tableView and there are no sub views in the tableview cell just the text, and it is not editable. –  May 22 '14 at 20:19