40media Reload model for UITableViewController before [tableview reloadData]


Reload model for UITableViewController before [tableview reloadData]



This is my first try into iPhone development and I have some UITableViewControllers which will show the data returned from a webservice.

In my AppDelegate I have setup a timer which is called every few seconds and reloads the model. I keep the same object reference and just refresh it's contents, so I have the same object on any UITableViewController that is visible at the moment.

When the data is refreshed on the AppDelegate, I call:

[[(UITableViewController *)[self.navigationController topViewController] tableView] reloadData]; 

The model is basically a collection of file objects. Each file object has some properties and flags.

This works quite well if the current UITableViewController is a simple table, with one section, that maps each cell to a sequential index on the Array of files.

But I have one of the UITableViewControllers that shows this file details and, depending on a flag on the file object, will show 2 or 3 sections. I've put this logic here:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {     if ([[_file status] intValue] & DOWNLOADING) {     	kIndexTransferSpeed = 1;     	kIndexSettings = 2;     	return 3;     } else {     	kIndexSettings = 1;     	kIndexTransferSpeed = -1;     	return 2;	     } } 

So on this controller, my -tableView:cellForRowAtIndexPath: is just a big if statement that will show a property from the _file object, depending on the indexPath.

The problem with this approach is that when this table is reloading on the device, I can actually see it being reloaded. The section titles flash for a moment and the cells take a bit to show up. Specially if I am scrolling the table while it's reloaded.

I've read somewhere that I should keep myself from putting any logic on those UITableViewController methods. But what should I do?

I thought about having a specific method that will create a "data view model" with the data ready to be consumed by - tableView:cellForRowAtIndexPath: and would be called by the AppDelegate before calling it's [tableView reloadData]. But I'd have to recreate this whole "view model" everytime it's called, since it's the same object and I don't really know what has been changed on the data model.




How do you get the viewport scale after pinch/zoom on an iPhone web app?

1:



asp.net Mobile implementation on IPhone, Blackberry and other mobile devices
reloadData has to handle lots of things, including sections coming and going, objects moving in relative order etc.


Using an Objective-C++ file from a C++ file
Without knowledge of what is actually going on there is sometimes nothing it can do but basically teardown and rebuilt the table..
Saving a string into file in Objective-C (iPhone)
If you want to avoid that you can instead inform the controller of the specific changes that have occured.


Storing a persistent CLLocation
Basically instead of your view reloading all the data through the dataSource in your controller, the controller tells it specifically what to up date and how..
Reading and Updating application settings from NSUserDefaults using Flipside View Controller
It is a lot more work than just calling reload, but it is more efficient, uses less power, and because the UI has better information it not only avoids the whole thing disappearing and reappearing, but can actually animate in the changes..
Select Multiple Images (UIImagePickerController or Photos.app Share UI)
The methods you want to look at are:.
Read and update a Dictionary type PSMultiValueSpecifier from NSUserDefault
- (void)beginUpdates;   // allow multiple insert/delete of rows and sections to be animated simultaneously. 

Nestable - (void)endUpdates; // only call insert/delete/reload calls inside an update block.

otherwise things like row count, etc.

may be invalid.

- (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation; - (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation; - (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation; - (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation; - (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation; - (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
More documentation is available here..



70 out of 100 based on 50 user ratings 850 reviews