Friday, June 7, 2013

UITableView reloadData example in Objective C (iOS).


UITableView reloadData

Reloads the rows and sections of the receiver.

- (void)reloadData

Discussion of [UITableView reloadData]
Call this method to reload all the data that is used to construct the table, including cells, section headers and footers, index arrays, and so on. For efficiency, the table view redisplays only those rows that are visible. It adjusts offsets if the table shrinks as a result of the reload. The table view'€™s delegate or data source calls this method when it wants the table view to completely reload its data. It should not be called in the methods that insert or delete rows, especially within an animation block implemented with calls to beginUpdates and endUpdates

UITableView reloadData example.
If you call reloadData from within a dispatched method, make sure to execute it on the main queue.

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND,0), ^(void) {

    // hard work/updating here

    // when finished ...
    dispatch_async(dispatch_get_main_queue(), ^(void) {
        [self.myTableView reloadData];
    });
});
..same in method form:

-(void)updateDataInBackground {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND,0), ^(void) {

        // hard work/updating here

        // when finished ...
        [self reloadTable];
    });
}

-(void)reloadTable {
       dispatch_async(dispatch_get_main_queue(), ^(void) {
            [myTableView reloadData];
        });
}

Example of [UITableView reloadData].
- (void)reloadTheTB {
   NSLog(@"Test reloadTheTB");
   NSLog(@"%@",dict);
  [[self tableView] reloadData];
}

Is the content of your dictionary what you expect? or is not changed after loadPlist?

Probably the call to reloadData is not executed on the main thread and you can update the UI only on the main thread. Try to execute your method on the main thread doing something like this :

-(void)ReloadTheTB{  
  [self.tableView performSelectorOnMainThread@selector(reloadData) withObject:nil waitUntilDone:NO]
}

UITableView reloadData example.
Use this code.

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self.tableView reloadData];
}
if fired when u click the button simply write this also

-(IBAaction) buttonclick
{
   [self.tableView reloadData];
   // your code...
}

End of UITableView reloadData example article.