【发布时间】:2017-02-13 06:20:42
【问题描述】:
我必须在 UITableViewCell 中单击相应按钮时从服务器下载文件。当用户点击按钮以在一个单元格中下载时,应该开始下载。下载完成后,我将保存核心数据。到目前为止一切顺利。但是在下载当前文件时,如果用户点击以在 Cell 中下载另一个文件,它也应该下载然后保存到核心数据并可供播放。而且我在每个表格单元格中有不同的 url。如果用户点击多个按钮应该下载它们并保存到核心数据。这是我的代码。
NSString *url=[[chatHistoryArr objectAtIndex:sender.tag]valueForKey:@"voice"];
NSLog(@"%@",url);
//NSURL *voiceUrl=[NSURL URLWithString:url];
tempDict=[[NSMutableDictionary alloc]init];
[tempDict setValue:[[chatHistoryArr objectAtIndex:sender.tag]valueForKey:@"msg_id"] forKey:@"msg_id"];
[tempDict setValue:[[chatHistoryArr objectAtIndex:sender.tag]valueForKey:@"to"] forKey:@"to"];
[tempDict setValue:[[chatHistoryArr objectAtIndex:sender.tag]valueForKey:@"from"] forKey:@"from"];
[tempDict setValue:[[chatHistoryArr objectAtIndex:sender.tag]valueForKey:@"time"] forKey:@"time"];
UIImageView* animatedImageView = [[UIImageView alloc] initWithFrame:cell.playButton.bounds];
animatedImageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"1.png"],
[UIImage imageNamed:@"2.png"],
[UIImage imageNamed:@"3.png"],
[UIImage imageNamed:@"4.png"], nil];
animatedImageView.animationDuration = 3.0f;
animatedImageView.animationRepeatCount = 10;
[animatedImageView startAnimating];
[cell1.playButton addSubview: animatedImageView];
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
[manager setResponseSerializer:[AFHTTPResponseSerializer serializer]];
// manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/octet-stream",@"video/3gpp",@"audio/mp4",nil];
NSURL *URL = [NSURL URLWithString:url];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
if (error)
{
NSLog(@"Error: %@", error);
} else
{
NSData *data=[[NSData alloc]initWithData:responseObject];
//Here I'm saving file to local storage then updating UI.
[self sentMsgSaveWithData:data orUrl:@"" withBool:YES withMsg_ID:@"" withDict:tempDict];
}
}];
[dataTask resume];
在这里,我设法一次只下载一个文件,完成后,如果用户点击另一个单元格,那么只有我正在下载它。但是我必须在单元格中的多个按钮点击上下载多个文件。
我一直在努力实现这一点。请提出一些建议。
提前致谢。
【问题讨论】:
-
您会将 url 传递给一种方法,比如说 openurl(NSUrl*) mypicUrl :(Int) cellrow。所以你知道在下载结束时必须重新加载哪个单元格。
标签: ios objective-c uitableview