2

The SimpleEKDemo sample has a "[self.tableView reloadData]" at the end of viewDidLoad in the RootViewController.m file.

Is this necessary? Any ideas why this line was put in? Wouldn't the view get drawn subsequently after the viewDidLoad via it's calls to the delegate to methods like "cellForRowAtIndexPath"?

- (void)viewDidLoad {
    self.title = @"Events List";

    // Initialize an event store object with the init method. Initilize the array for events.
    self.eventStore = [[EKEventStore alloc] init];

    self.eventsList = [[NSMutableArray alloc] initWithArray:0];

    // Get the default calendar from store.
    self.defaultCalendar = [self.eventStore defaultCalendarForNewEvents];

    //  Create an Add button 
    UIBarButtonItem *addButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:
                                UIBarButtonSystemItemAdd target:self action:@selector(addEvent:)];
    self.navigationItem.rightBarButtonItem = addButtonItem;
    [addButtonItem release];


    self.navigationController.delegate = self;

    // Fetch today's event on selected calendar and put them into the eventsList array
    [self.eventsList addObjectsFromArray:[self fetchEventsForToday]];

    [self.tableView reloadData];   // ** REALLY NEEDED ** 

}

EDIT - I did note this in the doco (see below) - it's still not clear to me how the above line of code is required - isn't it the case that if you went to another view, and then back to this view, THEN if (a) the view is re-initialised than it would have to populate itself again anyway, or (b) if the view wan't re-initialised the "viewDidLoad" method wouldn't be called hence you wouldn't be putting the "reloadData" line of code at the end of the viewDidLoad method in either case no?

UITableView overrides the layoutSubviews method of UIView so that it calls reloadData only when you create a new instance of UITableView or when you assign a new data source. Reloading the table view clears current state, including the current selection. However, if you explicitly call reloadData, it clears this state and any subsequent direct or indirect call to layoutSubviews does not trigger a reload.

Greg
  • 34,042
  • 79
  • 253
  • 454

1 Answers1

3

This is not really needed if you have static data but when you want to update the table content from a data source then you need.

e.g.

Suppose you want add a record which you enter in a table and you want to show that data in table when you add that.you have a table on first page then add a record on second view and come back to first view.then you need to show new data but this time your deta source method for table not called thats why you need to reload the data so this line calls table's data source method.

I think you can understand the situation.

Edit:

When we delete a row then that data delete from database but remains in table if you not call delete with table. now you are calling database function and getting record in an array at viewWillAppear function now what you can do simply call viewWillappear from commitEditingStyle function and reload the table in viewWillappear.At this situation you need to reload the table

Ishu
  • 12,797
  • 5
  • 35
  • 51
  • umm, that makes me realize I'm not sure how ofter "viewDidLoad" is actually called - is it called each time you go from an edit view back to itself again for example? And if it doesn't wouldn't this highlight an issue as the code would be alloc'ing variables such as EKEventStore often then, where apple said you should avoid recreating this too often as it's resource intensive – Greg Feb 28 '11 at 05:10
  • @Ishu - but I'm not 100% convinced it correct and no-one else has upvoted yet? I just commented out the line in the SimpleEKDemo, and then dynamically added some data (clicking on the ADD button in the demo), then when it goes back to the table view it is up to date (i.e. without the [self.tableView reloadData] in place) – Greg Feb 28 '11 at 08:50
  • @Greg,please tell me your sdk.i think in hiegher version viewDidLoad not calls again when you go back.so at that time we write this code in viewWillappear. Apple never loads a page more than one .if viewDidLoad calls then this is also true, No need to reload data.But when i use this with 3.1.3 and 4.2,app needs reload itself in viewWillappear and viewDidLoad not called – Ishu Feb 28 '11 at 08:57
  • I'm running at the latest 4.2 version Ishu – Greg Feb 28 '11 at 10:40
  • Ok then, i think you dont need table reload function if viewDidLoad calls.In case if viewDidLoad not calls then you need to reload data. – Ishu Feb 28 '11 at 10:47
  • Ok i am having one more situation whem you need to reload the table. – Ishu Feb 28 '11 at 10:47
  • which is that situation Ishu? – Greg Feb 28 '11 at 11:20