Creo que esta es una pregunta bastante compleja. Tengo un TableView que muestra una cantidad de contenido descargable. Cuando hace clic en un botón dentro de la celda, comienza la descarga.Forma correcta de mostrar contenido descargable en UITableView (con ProgressBar etc.)
pero estoy teniendo varios problemas: 1.How puedo hacer para asegurar que el progressBar se mostrará todo el tiempo (incluso si se volverá a cargar los rollos usuario se desplaza y la célula) 2.How puedo hacer Seguro que el usuario puede descargar 2 archivos a la vez. Me temo que causa problemas porque utilizo algunas variables de instancia. En una forma en que debe trabajar un poco como descarga desde iCloud en la aplicación Music
Aquí está mi código
- (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];
}
Sería bueno si alguien tiene una idea o un buen ejemplo de código
Si necesita la barra de progreso que se muestra todo el tiempo, es probable que no desee colocarla en una celda de la tabla. ¿O quiere decir que siempre muestra cuándo está visible la celda en la ruta de índice del elemento de descarga? –
Quiero mostrar la barra de progreso en la celda correspondiente – arnoapp