1

I'm trying to reload the values of a table view after exiting from a seque. The process being: I perform the seque manually from the profile selection view, add a new profile name, return to the profile selection view. Then I would like to reload the table view adding the new profile name. It is running the code fine (same code as original entry into the scene), but I can't seem to get the native methods of numberOfRowsInSection and numberOfRowsInSection to repopulate the table view. I actually have to leave the screen and reenter it before the new profile name will update. Any thoughts?


//** performing seque manually

-(IBAction)buttonAddNewProfile:(id)sender
{
    // creating object for profile selection screen
    UIStoryboard *ProfileSelectionStoryboard=[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];

    // creating object for add new profile storyboard
    AddNewProfileViewController *addnewprofileVC=[ProfileSelectionStoryboard instantiateViewControllerWithIdentifier:@"Add New Profile"];

    // setting the transition style
    addnewprofileVC.modalTransitionStyle=UIModalTransitionStylePartialCurl;

    // performing the segue
    [self presentViewController:addnewprofileVC animated:YES completion:nil];

    // performing new table view load on return from new profile
    [self loadUsers];
}

//** function to load the new profile names in.

-(void)loadUsers
{
    // retreiving the users from the database
    SQLiteFunctions *sql = [[SQLiteFunctions alloc] init];

    // testing for successful open
    if([sql openDatabase:@"users"])
    {
        // setting query statement
        const char *query = "SELECT * FROM  users;";

        // testing for that profile name existing already
        if([sql getUserRecords:query] > 0)
        {
            // initializing array
            NSMutableArray *names = [[NSMutableArray alloc] init];

            // loop through object compling an array of user names
            for(Users *ProfileUser in sql.returnData)
            {
                // adding user name to the listview array
                [names addObject:ProfileUser.user_name];
            }

            // setting table view array to local array
            tableData = names;
        }
    }
}

//** methods to reload the table view

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
    // returning the number of rows in the table
    return [tableData count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    // setting up the table view cells for data population
    UITableViewCell *cell = nil;
    cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell"];

    // testing for cell parameters
    if (cell == nil)
    {
        // setting up cloned cell parameters
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyCell"];
    }

    // setting cell values to the array row value
    cell.textLabel.text = [tableData objectAtIndex:indexPath.row];

    // returning the current row label value
    return cell;
}
Grymjack
  • 529
  • 1
  • 10
  • 21

2 Answers2

3

You have a few different options here:

1) The easiest is to simply reload the table every time that the view controller is about to display its view:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.tableView reloadData];
}

The downside though, is that this will be executed every time that the view is displayed, even when you don't necessarily need to reload the data.

2) If you are using storyboard's and targeting iOS 6+ then you can use an unwind segue to call a specific method on your view controller when going back from the add profile view controller. For more info, see this SO question/answers: Does anyone know what the new Exit icon is used for when editing storyboards using Xcode 4.5?

3) If you are targeting older versions of iOS or aren't using storyboards, then you can create a protocol with a method that should be called whenever a new profile is added and you can reload the data whenever that method is called. There are lots of questions here on SO which cover how to do this (like dismissModalViewController AND pass data back which shows how to pass data, but you can do the same thing to just call a method).

Community
  • 1
  • 1
lnafziger
  • 25,760
  • 8
  • 60
  • 101
  • This is perfect, thanks. Your links are also helpful as I am using a lot of segues in a navigation intensive app. I was looking for a graceful way to avoid the continual instancing of new views and instead have a 'home' view that would act as a central point for seques. – Grymjack Dec 28 '12 at 19:19
  • Just got a chance to get behind my computer again. According to the debugger, this method isn't hit as it exits from the segue. -(void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self loadUsers]; [self.userTableView reloadData]; } – Grymjack Dec 28 '12 at 20:01
  • It enters when I first open the view, but not exiting the segue. – Grymjack Dec 28 '12 at 20:08
  • Really? It should be. You put that in the original view controller and not in the add profile controller, right? – lnafziger Dec 28 '12 at 20:46
  • It is in the ProfileSelectionViewController.m It segues off to the AddNewProfileViewController, adds a new profile, then returns. The breakpoint I put in the function was hit once when entering from the title screen, then never touched again. If it would help I could post all the applicable code in an answer to this question? – Grymjack Dec 29 '12 at 02:30
  • 1
    It took me a bit to figure this out, but it is because you are using the `UIModalTransitionStylePartialCurl` `modalTransitionStyle`. I guess they don't call it because it doesn't completely leave the screen. If you use a different style (like the default) then it will call `viewWillAppear`. If you want to stay with that, then you should probably use one of the other methods. – lnafziger Dec 29 '12 at 21:00
  • ahhh....I'm not attached to that style. Just tested it, works great! Thanks again for your help, I appreciate your time spent. – Grymjack Dec 30 '12 at 02:13
0

This the actual answer from lnafzinger in the comments above. Thanks again.

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.tableView reloadData];
}

It took me a bit to figure this out, but it is because you are using the UIModalTransitionStylePartialCurl modalTransitionStyle. I guess they don't call it because it doesn't completely leave the screen. If you use a different style (like the default) then it will call viewWillAppear. If you want to stay with that, then you should probably use one of the other methods. – lnafziger

Grymjack
  • 529
  • 1
  • 10
  • 21
  • By the way, since this is technically not an answer, you should edit your original question and add the code there instead of putting it in an answer. Most people won't look at the answers for more details like this. – lnafziger Dec 30 '12 at 02:50
  • *UIModalTransitionStyleFlipHorizontal* and *UIModalTransitionStyleCrossDissolve* work fine. I haven't tried any of the others. – Grymjack Dec 30 '12 at 13:06
  • Welcome to the question that never ends `-(void)viewWillAppear:(BOOL)animated works fine` However. you can't launch another segue from it, you need to use `-(void)viewDidAppear:(BOOL)animated` I guess the old segue hasn't totally finished yet? – Grymjack Dec 31 '12 at 02:23
  • Actually, a segue wasn't part of the original question. If you were having a problem with that, you could have asked another question! :) Also, if you want me to get the points for helping you, since I gave you the answer, you need to accept my answer instead of yours.... – lnafziger Dec 31 '12 at 03:22
  • sorry the segue was mentioned in the other post you just mentioned that I should delete :) – Grymjack Dec 31 '12 at 04:29
  • I have 7 views that I need to navigate between from any of the views. I don't want to keep doing segues from each one as it would keep creating new instances. I had a home view I would return to after setting a public variable on which view it should segue to (if any) upon returning from the old segue. `-(void)viewDidAppear:(BOOL)animated` in the HomeController worked fine as a point to perform the check for a resegue. However, you see the home view before it moves to the new one. `-(void)viewWillAppear:(BOOL)animated` won't work for segues as you can't segue from there until it exits. – Grymjack Dec 31 '12 at 04:33
  • And yes I am appreciating Stack Overflow tremendously. I had many questions answered by searching without ever having to post a question myself. It is great resource that I plan to contribute to as I gain more skill in iOS programming. – Grymjack Dec 31 '12 at 04:36
  • as an additional note the `-(void)viewWillAppear:(BOOL)animated` solution you provided worked great for my original problem of the 'profile selection screen'. I was just trying to adapt the solution to my segue management problem and ran into the timing issue. Sorry for the choppy responses, just trying to find time to sit down and code during the holiday season (bah humbug). – Grymjack Dec 31 '12 at 05:18
  • @lnafziger, I took your advice and asked a brand new question. [http://stackoverflow.com/questions/14099002/xcode-segue-jump-management-in-a-app-with-many-views][2] [2]: http://many%20view%20segues – Grymjack Dec 31 '12 at 07:57