Wednesday, July 11, 2012

Lazy loading Custom UITableViewCell

this tutorial says how to lazy load or Asynchronous load custom table cell image so the scrolling of table cell is efficient.i have taken help from  apple’s sample code for lazy loading or asynchronous loading and use for custom table cell.
first crate view controller and create table view as the way you want, i have created view controller and added table view.Now in view controller ,since we are requesting to url for our data ,make NSURLConnection as below
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:xmlDataUrl]];
self.connection = [[[NSURLConnection alloc] initWithRequest:urlRequest delegate:self] autorelease];
since we have set delegate of connection to self to it will return to following method after data is loaded.


- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
self.connection = nil;   // release our connection
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
 
// create the queue to run our ParseOperation
self.queue = [[NSOperationQueue alloc] init];
ParseOperation *operation = [[ParseOperation alloc] initWithData:self.xmlData delegate:self];
[queue addOperation:operation]; // this will start the "ParseOperation"
[operation release];
self.xmlData = nil;
 
}
in delegate method create NSOperation and parse the xml in operation
After parsing is completed populate the table view.
since main work here is loading the image in table cell
dowloading the image is the same using NSURLConnection and calling appropriate method delegate method after image data is received.
the code can be downloaded from link below
LazyLoadTableImages in custom cell

No comments:

Post a Comment