1

I have an UIViewController subclass where I have an UITableView. This is the way I use viewDidLoad & viewWillAppear so far.

-(void)viewDidLoad
{
    //Setup my datasource
    //Setup my views, tableviews, constraints
}

-(void)viewWillAppear:(BOOL)animated
{
    //Setup my datasource
}

viewDidLoad will be called only once when the view is constructed. viewWillAppear will be called everytime I visit the view controller.

Here, why I setup my datasource(NSArray) in both places is, whenever I come into the viewcontroller, I just need to reconstruct the datasource array.

I can simply do like this.

-(void)viewDidLoad
{
    //Setup my views, tableviews, constraints
}

-(void)viewWillAppear:(BOOL)animated
{
    //Setup my datasource
    //Reload tableview
}

But, This feels bit slow in showing data when the view controller appears.

Questions:

What is the best practice to setup the datasource array? I don't want a delay by setting it only in viewWillAppear. Or should I setup the datasource like this?

-(void)viewDidLoad
    {
        //Setup my datasource
        //Setup my views, tableviews, constraints
    }

    -(void)viewWillAppear:(BOOL)animated
    {
        //Setup my datasource
        //Reload tableview
    }

If YES, for the first time, I need to calculate the datasource array two times.

To overcome this issue, I need to keep a BOOL value like isFirstTime (stored in NSUserDefaults) by setting it in viewDidLoad and check it in viewWillAppear method as like this:

        -(void)viewDidLoad
        {
            //Set isFirstTime as YES
            //Setup my datasource
            //Setup my views, tableviews, constraints
        }

        -(void)viewWillAppear:(BOOL)animated
        {
            if(isFirstTime)
            {
               //Skip
               isFirstTime = NO;
            }

            else
            {
               //Setup my datasource
               //Reload tableview
            }
        }

Should I really do this much complex things to achieve this? Suggestions needed!!

Thanks

Confused
  • 3,846
  • 7
  • 45
  • 72

1 Answers1

0

Just set up the view and datasource in viewdidload.You don't need to setup datasource gain in viewwillappear and you even don't need the reload data in view willappear as table automatically reloads itself when view is rendered again

Ahmad Ishfaq
  • 914
  • 1
  • 8
  • 10
  • No, the table view will not be reloaded automatically. – rmaddy May 12 '16 at 00:08
  • @rmaddy Just to be clear when ios initializes a view controller let's say this one has the tableview and than you push another controller and come back to the previous one than ios will automatically call tableview delegate again and render the tableview no need to call tableview reload – Ahmad Ishfaq May 12 '16 at 00:44
  • Your last statement is incorrect. It is possible that a `UITableViewController` does this, but a regular `UIViewController` certainly does not. – Avi May 12 '16 at 04:50
  • @Avi try putting breakpoints in cellforrow and create the above scenario that i explained to maddy and you will understand – Ahmad Ishfaq May 12 '16 at 12:46