I think this a rather complex question. I have a TableView that displays a number of downloadable content. When you click it on a button within the cell the download starts.
But I'm having several issues: 1.How can I make sure that the progressBar will be shown all the time (even if the user scrolls scrolls and the cell will be reloaded) 2.How can I make sure that the user can download 2 files at once. I'm afraid it causes issues because I use some instance Variables. In a way it should work a bit like downloading from iCloud in the Music App
Here is my code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil]; } //cell.tag = indexPath.row*10; Uebungsblaetter *uebungCell = [uebungsblattArray objectAtIndex:indexPath.row]; cell.tag = indexPath.row*10; cell.textLabel.text = [self getFileNameOutOf:uebungCell.url]; cell.textLabel.textColor = [UIColor grayColor]; cell.selectionStyle = UITableViewCellSelectionStyleNone; UIButton *dl = [UIButton buttonWithType:UIButtonTypeCustom]; dl.tag = indexPath.row*10; [dl setBackgroundImage:[UIImage imageNamed:@"downloadButton.png"] forState:UIControlStateNormal]; [dl setBackgroundImage:[UIImage imageNamed:@"downloadButtonH.png"] forState:UIControlStateHighlighted]; [dl setFrame:CGRectMake(230.0, (cell.frame.size.height-28)/2, 28, 28)]; [dl addTarget:self action:@selector(downloadFileWhenPressedButton:) forControlEvents:UIControlEventTouchUpInside]; [cell.contentView addSubview:dl]; UIProgressView *dlProgress = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault]; dlProgress.frame = CGRectMake(cell.frame.size.width-150, 17, 50, 9); dlProgress.tag =indexPath.row*10+1; dlProgress.progress = 0.0; [cell.contentView addSubview:dlProgress]; [dlProgress setHidden:YES]; return cell; } //download methods - (void)downloadFileWhenPressedButton:(UIButton*)sender{ sender.hidden = YES; dlIndex = sender.tag/10; Uebungsblaetter *selectedUB = [uebungsblattArray objectAtIndex:dlIndex]; NSURL *theUrl = [NSURL URLWithString:selectedUB.url]; NSURLRequest *req=[NSURLRequest requestWithURL:theUrl cachePolicy:NSURLCacheStorageNotAllowed timeoutInterval:120]; dlCell = (UITableViewCell *)[[sender superview]superview]; currDlProgress = (UIProgressView* )[dlCell.contentView viewWithTag:dlIndex*10+1]; currDlProgress.hidden = NO; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); dlFilePath = [NSString stringWithFormat:@"%@/%@_%@", [paths objectAtIndex:0],self.courseLable.text,[self getFileNameOutOf:selectedUB.url]]; NSURLConnection *con=[[NSURLConnection alloc] initWithRequest:req delegate:self startImmediately:YES]; if (con) { myWebData = [NSMutableData data]; } } - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ UIApplication* app = [UIApplication sharedApplication]; app.networkActivityIndicatorVisible = YES; currDlProgress.progress = 0; _totalFileSize = response.expectedContentLength; NSLog(@"%@",@"connection established"); [myWebData setLength: 0]; } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { _receivedDataBytes += [data length]; currDlProgress.progress = _receivedDataBytes / (float)_totalFileSize; NSLog(@"%@",@"connection receiving data"); [myWebData appendData:data]; } - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { NSLog(@"%@",@"connection failed"); // [AlertViewHandler showAlertWithErrorMessage:@"Sorry, there is no network connection. Please check your network and try again."]; // [self parserDidEndDocument:nil]; } - (void)connectionDidFinishLoading:(NSURLConnection *)connection { UIApplication* app = [UIApplication sharedApplication]; app.networkActivityIndicatorVisible = NO; [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.35]; [currDlProgress setAlpha:0]; [UIView commitAnimations]; [myWebData writeToFile:dlFilePath atomically:YES]; Uebungsblaetter *loadedUB = [uebungsblattArray objectAtIndex:dlIndex]; loadedUB.downloaded = [NSNumber numberWithBool:YES]; [courseTable reloadData]; } Would be nice if somebody has a clue or a nice code example
source: stackoverflow.com
No comments:
Post a Comment