显示大量可下载内容的TableView。当我在单元格中的一个按钮上单击它时,下载开始。我可以一次下载一个文件(我收到的文件很好)。但是,如果我一次下载多个文件,我收到的文件都会混淆。如何更改此代码以便能够一次下载多个文件。
这是我的代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = @"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:MyIdentifier];
}
RPlayList * tanRPlayList = (dloadList)[indexPath.row];
rowID = tanRPlayList.xid;
cell.textLabel.text = tanRPlayList.name;
cell.detailTextLabel.text = tanRPlayList.khname;
cell.imageView.image = [UIImage imageNamed:tanRPlayList.stationImage];
// remove any previous buttons or progress bars from this cell
for (UIView *view in [cell.contentView subviews]) {
if ([view isKindOfClass:[UIProgressView class]] || [view isKindOfClass:[UIButton class]]) {
[view removeFromSuperview];
}
}
// look for active connecton for this cell
NSMutableDictionary *dict = [self getConnectionInfoForRow:indexPath.row];
if (dict) {
// there is an active download for this cell, show a progress bar
UIProgressView *dlProgress = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
dlProgress.frame = CGRectMake(cell.frame.size.width-60, 10, 50, 9);
dlProgress.tag = indexPath.row; // * 10 + 1;
float fNumber;
fNumber = (float)[[dict objectForKey:@"receivedBytes"]intValue] / (float)[[dict objectForKey:@"totalFileSize"]intValue];
dlProgress.progress = fNumber;
if((int)fNumber == 1){
dlProgress.hidden = YES;
}
[cell.contentView addSubview:dlProgress];
} else {
UIButton *dl = [UIButton buttonWithType:UIButtonTypeCustom];
dl.tag = indexPath.row; // * 10;
[dl setBackgroundImage:[UIImage imageNamed:@"btndownload_06.png"] forState:UIControlStateNormal];
dl.frame = CGRectMake(215, 10, 100, 30);
[dl addTarget:self action:@selector(btnDLoadSelected:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:dl];
}
return cell;}
- (void)btnDLoadSelected:(UIButton*)sender{
UIButton *btn = (UIButton*)sender;
int selected_index = btn.tag;
RPlayList * tanRPlayList = (dloadList)[selected_index];
NSString *cellValueRowID = tanRPlayList.xid;
NSString *cellValueName = tanRPlayList.name;
NSString *cellValueKHName = tanRPlayList.khname;
NSString *cellValuePLink = tanRPlayList.plink;
NSString *kSort = tanRPlayList.sort;
NSString *iconName = tanRPlayList.stationImage;
[btn setHidden:YES];
NSURL *url = [NSURL URLWithString:cellValuePLink];
NSURLRequest *req = [NSURLRequest requestWithURL:url cachePolicy:NSURLCacheStorageNotAllowed timeoutInterval:120];
NSURLConnection *con=[[NSURLConnection alloc] initWithRequest:req delegate:self startImmediately:YES];
// then create dictionary
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject:con forKey:@"connection"]; // save connection so we can reference later
[dict setObject:[NSNumber numberWithInt:sender.tag] forKey:@"row"]; // this is the row index from your table
[dict setObject:[NSNumber numberWithInt:999] forKey:@"totalFileSize"]; // dummy size, we will update when we know more
[dict setObject:[NSNumber numberWithInt:0] forKey:@"receivedBytes"];
NSString *fileName = [cellValuePLink lastPathComponent];
DLOAD_PATH = [NSString stringWithFormat:@"%@/%@",DLOAD_PATH0,fileName];
[self.activeConnections addObject:dict];
if(con){
myWebData = [NSMutableData data];
}
NSString *stationName = [cellValueName substringToIndex:3];
[appDelegate.mKhemaDB beginTransaction];
//NSLog(@"I am at End btnDLoadSelected ...");
}
- (NSMutableDictionary*)getConnectionInfo:(NSURLConnection*)connection{
for (NSMutableDictionary *dict in self.activeConnections) {
if ([dict objectForKey:@"connection"] == connection) {
return dict;
}
}
return nil;
}
- (NSMutableDictionary*)getConnectionInfoForRow:(int)row{
for (NSMutableDictionary *dict in self.activeConnections) {
if ([[dict objectForKey:@"row"] intValue] == row) {
return dict;
}
}
return nil;
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
UIApplication* app = [UIApplication sharedApplication];
app.networkActivityIndicatorVisible = YES;
NSMutableDictionary *dict = [self getConnectionInfo:connection];
[dict setObject:[NSNumber numberWithInt:response.expectedContentLength] forKey:@"totalFileSize"];
[myWebData setLength: 0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSMutableDictionary *dict = [self getConnectionInfo:connection];
//NSNumber bytes = [data length] + [[dict objectForKey:@"receivedBytes"] intValue];
NSInteger bytes = [data length] + [[dict objectForKey:@"receivedBytes"] intValue];
//[dict setObject:[NSNumber numberWithInt:response.expectedContentLength] forKey:@"receivedBytes"];
[dict setObject:[NSNumber numberWithInt:bytes] forKey:@"receivedBytes"];
int row = [[dict objectForKey:@"row"] intValue];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0];
[self.aTableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationNone];
[myWebData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
//Write file to folder
NSMutableDictionary *dict = [self getConnectionInfo:connection];
[self.activeConnections removeObject:dict];
[myWebData writeToFile:DLOAD_PATH atomically:YES];
NSLog(@"Finished DLoaded File!");
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [aDloadList count];
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
UIButton *b = nil;
for(UIView *v in cell.subviews) {
if([v isKindOfClass:[UIButton class]]) {
b = (UIButton*)v;
break;
}
}
b.tag = indexPath.row;
}
答案 0 :(得分:0)
使用ASINetworkQueue。它将允许您创建仍可以异步启动的ASIHTTPRequests队列。例如:
- (void)getImages
{
if(!self.queue)
self.queue = [[[ASINetworkQueue alloc] init] autorelease];
NSArray* urlStringsToRequest = [NSArray arrayWithObjects:@"http://www.example.com/image1.png",@"http://www.example.com/image2.png",nil];
for(NSString* urlString in urlStringsToRequest)
{
NSURL *url = [NSURL URLWithString:urlString];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request setDidFinishSelector:@selector(requestDone:)];
[request setDidFailSelector:@selector(requestWentWrong:)];
[self.queue addOperation:request];
}
[self.queue go];
}
- (void)requestDone:(ASIHTTPRequest*)req
{
UIImage* image = [UIImage imageWithData:[req responseData]];
[imageArray addObject:image];
}
- (void)requestWentWrong:(ASIHTTPRequest*)req
{
NSLog(@"Request returned an error %@",[req error]);
}
谢谢,
答案 1 :(得分:0)
NSMutableData *myWebData1 = (NSMutableData *)[dict objectForKey:@"myMutData"];
[myWebData1 appendData:data];
- (void)btnDLoadSelected:(UIButton*)sender{
//.....
// then create dictionary
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject:con forKey:@"connection"]; // save connection so we can reference later
[dict setObject:[NSNumber numberWithInt:sender.tag] forKey:@"row"]; // this is the row index from your table
[dict setObject:[NSNumber numberWithInt:999] forKey:@"totalFileSize"]; // dummy size, we will update when we know more
[dict setObject:[NSNumber numberWithInt:0] forKey:@"receivedBytes"];
[dict setObject:[NSMutableData alloc] forKey:@"myMutData"];
[self.activeConnections addObject:dict];
if(con){
myWebData = [[NSMutableData data]initWithLength:0];
}
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
// ...
NSMutableDictionary *dict = [self getConnectionInfo:connection];
[dict setObject:[NSNumber numberWithInt:response.expectedContentLength] forKey:@"totalFileSize"];
myWebData = [[NSMutableData data]initWithLength:0];
[dict setObject:[NSMutableData alloc] forKey:@"myMutData"];}
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
// ...
NSMutableDictionary *dict = [self getConnectionInfo:connection];
NSInteger bytes = [data length] + [[dict objectForKey:@"receivedBytes"] intValue];
[dict setObject:[NSNumber numberWithInt:bytes] forKey:@"receivedBytes"];
if(!myWebData)
myWebData = [NSMutableData data];
NSMutableData *myWebData1 = (NSMutableData *)[dict objectForKey:@"myMutData"];
[myWebData1 appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// ...
//Write file to folder
NSMutableDictionary *dict = [self getConnectionInfo:connection];
[self.activeConnections removeObject:dict];
NSData *myData1 = [dict objectForKey:@"myMutData"];
[myData1 writeToFile:DLOAD_PATH atomically:YES];
}