Loading screen while UITableView performs "tableView:heightForRowAtIndexPath:"
I am using tableView:heightForRowAtIndexPath:
to set a custom height for each table cell in a UITableView
. This is an expensive operation but I have not found a way to speed it up yet (In the future I will be trying some of the methods mentioned by taber).
For now I would just like to display a loading screen of some sort. it does not need to be animated, as long as it is visible while the table view is being loaded. Is this possible?
EDIT: I have tried the loading screen method mentioned in taber's answer, but开发者_StackOverflow中文版 since it runs in the same thread as the UITableView it does not appear until the table view is finished loading and thus not very useful in this situation :)
Try #3:
You can add an activity indicator as a new view on top of your table view... something like: http://tapadoo.com/2009/iphone-how-to-do-full-screen-activity-status - then the trick would be figuring out when it's actually finished. I guess in your heightForRowAtIndexPath: method you could keep some kind of row count and check if that count is >= your data model row count. Does that do the trick?
Try #2:
Try to comment out your overridden tableView:heightForRowAtIndexPath: method completely, and instead where you create your cells in cellForRowAtIndexPath: try something like this:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CGRect contentRectTall = CGRectMake(0.0, 0.0, 302.0, 140.0);
CGRect contentRectMed = CGRectMake(0.0, 0.0, 302.0, 70.0);
CGRect contentRectSmall = CGRectMake(0.0, 0.0, 302.0, 42.0);
... cell dequeue etc ...
if ( ...the type of this cell is kRowTypeTall for example... ) {
cell = [[[UITableViewCell alloc] initWithFrame:contentRectTall reuseIdentifier:CellIdentifier] autorelease];
...
} else if ( ...the cell type is kRowTypeMed ) {
...
} else if ( ...the cell type is kRowTypeSmall ) {
...
}
... other cell customization ...
}
Other than that, you might want to look into "lazy loading" where you'd load say the first 50 cells, then add a "load 50 more" type of button at the end.
If you already know your row type (which is contained in some kind of data source) it should be pretty quick to pull out.
Something like...
enum {
kRowTypeHuge = 0,
kRowTypeMed,
kRowTypeSmall
};
-(id)init {
...
NSArray *tableRows = [[NSArray alloc] initWithObjects:
[NSNumber numberWithUnsignedInt: 0],
[NSNumber numberWithUnsignedInt: 1],
[NSNumber numberWithUnsignedInt: 0],
[NSNumber numberWithUnsignedInt: 0],
[NSNumber numberWithUnsignedInt: 2],
nil
];
self.rows = tableRows;
[tableRows release];
...
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger rowType = [[[self.rows] objectAtIndex: indexPath.row] unsignedIntValue];
if(rowType == kRowTypeHuge) {
return 105.0;
} else if(rowType == kRowTypeMed) {
return 44.0;
}
return 20.0;
}
To be positive that heightForRowAtIndexPath is the issue... does something like this render super fast?
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 52.0;
}
精彩评论