我认为这是一个相当复杂的问题。我有一个显示许多可下载内容的 TableView。当您单击单元格内的按钮时,下载开始。
但是我遇到了几个问题:1.如何确保始终显示进度条(即使用户滚动滚动并且重新加载单元格) 2.如何确保用户可以下载2个文件一次。我担心它会导致问题,因为我使用了一些实例变量。在某种程度上,它应该有点像在音乐应用程序中从 iCloud 下载
这是我的代码
- (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];
}
如果有人有线索或很好的代码示例会很好