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.

4

1 回答 1

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

于 2011-02-28T05:01:36.657 回答