0

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.

Code:

- (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];
}
user717452
  • 33
  • 14
  • 73
  • 149
  • Is your question about adding a progress bar to a tableviewcell, or is it about getting the progress bar to show progress? The code you show does not seem to jibe with the question in the title. – sosborn Mar 12 '12 at 23:59
  • I am asking about adding a progress bar to a tableviewcell. The code I posted is the code that will start the download. I posted some progress bar code for the download. I adapted this code to run in a normal view controller where I had added a progress bar using IB and it downloads and shows the progress fine. Just can't quite figure out how to show a progress bar in the selected table view cell when download starts, have it show the progress, and then hide after the download is complete. – user717452 Mar 13 '12 at 00:07
  • Every tableviewcell has a view called contentView. Just add the progressBar to that contentView as you would any other view. – sosborn Mar 13 '12 at 00:49
  • Ok, I have created a UIProgressView called progress in the header file, and for the cell, added: CGRect frame = CGRectMake(0, 49, 160, 50); progress = [[UIProgressView alloc] initWithFrame:frame]; [cell.contentView addSubview:progress]; The code for didSelect and all the NSURLConnection codes are the same as OP. The progress bar shows, but is in every cell, doesn't get hidden when download starts, and doesn't move at all. What could I do get it to only be in the cell selected, and hidden at all times but during download? – user717452 Mar 13 '12 at 02:19
  • 1
    You are on the right track, but it sounds like you are not clear on some of the basics of Object Oriented Programming. I think you are making a new UIProgressView instance every time you add one to a cell, so your references are all out of whack. You need to make sure that you call your methods on the exact instance for that particular tableviewcell. – sosborn Mar 13 '12 at 02:57
  • Are you sure that you add a UIProgressView in a didSelectRowAtIndexPath,not in cellForIndexPath? – Nikita Pestrov Mar 26 '12 at 14:55
  • ok @NikitaPestrov but how will you know which progress bar to show the progress???if i click on two cells then how to identify the which progressbar belongs to which cell???i have tried alot but i m not getting any solution please help me. – VK.Dev Aug 31 '12 at 16:14

1 Answers1

6

First, add an UIProgressView variable to you interface, so that you can reference to it from any method in your class.

Then, when user selects it. you have to initialize it

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    UITableViewCell *cell =[tableView cellForRowAtIndexPath:indexPath];

    progress = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleBar];
    progress.frame = CGRectMake(10, 10, 30, 30);
    progress.progress = 0.0;

    progress.center = CGPointMake(23,21);

    [cell.contentView addSubview:progress];


}

Now you can update a progressBar as the file downloads and after it just send[progress removeFromSuperView]

That's it!

Nikita Pestrov
  • 5,876
  • 4
  • 31
  • 66