PDA

View Full Version : Progress View in Table View Cell




newtoiphonesdk
Mar 12, 2012, 06:11 PM
I have an app that presents Table View of different files. I have it set up to Asynchronously download, and would like to have the selected cell show a progress bar. I've looked online all afternoon, and everything I have found so far seems real incomplete on how to do this. Here is my code so far for the download, and set up to have a progressview show download status.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
RSSEntry *entry = [_allEntries objectAtIndex:indexPath.row];
NSURL *url = [NSURL URLWithString:entry.articleUrl];
self.nameit = entry.articleTitle;
NSURLRequest *theRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
receivedData = [[NSMutableData alloc] initWithLength:0];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self startImmediately:YES];
}
- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
progress.hidden = NO;
[receivedData setLength:0];
expectedBytes = [response expectedContentLength];
}

- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[receivedData appendData:data];
float progressive = (float)[receivedData length] / (float)expectedBytes;
[progress setProgress:progressive];


}

- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[connection release];
}

- (NSCachedURLResponse *) connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse {
return nil;
}

- (void) connectionDidFinishLoading:(NSURLConnection *)connection {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:[currentURL stringByAppendingString:@".mp3"]];
NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[receivedData writeToFile:pdfPath atomically:YES];
progress.hidden = YES;
[connection release];
}



seepel
Mar 13, 2012, 12:15 AM
It seems you would want to have some sort of linked list that links progress bars with NSIndexPaths so you can have multiple cells downloading at the same time.

Another trick that I've implemented is to throw a category on UIApplication where you can store an integer that will keep track of how many concurrent connections are running and start the activity indicator when this value is greater than one, and stop the activit indicator when it becomes less than one.

You then give UIApplication the following functions.


static int ActivityCount = 0;
- (void)incrementActivityCounter {
if(ActivityCount == 0)
self.networActivityIndicator = YES;
ActivityCount++;
}

- (void)incrementActivityCounter {
ActivityCount++;
if(ActivityCount == 0)
self.networActivityIndicator = No;
}

newtoiphonesdk
Mar 13, 2012, 03:55 PM
It seems you would want to have some sort of linked list that links progress bars with NSIndexPaths so you can have multiple cells downloading at the same time.

Another trick that I've implemented is to throw a category on UIApplication where you can store an integer that will keep track of how many concurrent connections are running and start the activity indicator when this value is greater than one, and stop the activit indicator when it becomes less than one.

You then give UIApplication the following functions.


static int ActivityCount = 0;
- (void)incrementActivityCounter {
if(ActivityCount == 0)
self.networActivityIndicator = YES;
ActivityCount++;
}

- (void)incrementActivityCounter {
ActivityCount++;
if(ActivityCount == 0)
self.networActivityIndicator = No;
}


So far I am just doing one download at a time. The issue I am currently having is that when I click a cell to download, a progressview appears for that cell, and every other 8 cells as well. I'm not for sure how to get it to only show the progressview on one.

PhoneyDeveloper
Mar 13, 2012, 06:00 PM
You haven't shown any code for how you add the progress bar to the selected cell.

Sykte
Mar 14, 2012, 07:24 AM
So far I am just doing one download at a time. The issue I am currently having is that when I click a cell to download, a progressview appears for that cell, and every other 8 cells as well. I'm not for sure how to get it to only show the progressview on one.


Show your tableview delegate callbacks. Also you are potentially blocking your main thread.

seepel
Mar 15, 2012, 01:14 AM
Ultimately you need some way of keeping track of with cell needs a progress view, then make sure that only that cell displays the progress view. This will be a combination of code from your NSURLConnection delegate methods (to add remove and updat the progress) and your your table view data source method providing the cell.