如何在表格单元格中显示进度?
Posted
技术标签:
【中文标题】如何在表格单元格中显示进度?【英文标题】:How to show progress in table cell? 【发布时间】:2016-04-01 14:00:47 【问题描述】:我从服务器下载一个文件。文件下载时,我想在表格单元格中显示进度百分比。我该怎么做?
下载文件代码
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"01.mp3"];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:NO];
if (!fileExists)
NSString *stringURL = @"https://drive.google.com/uc?export=download&id=0B6zMam2kAK39X0FOaUJXVDUwOHc";
NSURL *url = [NSURL URLWithString:stringURL];
NSData *urlData = [NSData dataWithContentsOfURL:url];
[urlData writeToFile:filePath atomically:YES];
);
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:filePath] error:nil];
【问题讨论】:
【参考方案1】:[NSData dataWithContentsOfUrl]
无法做到这一点。您需要一个库,例如 AFNetworking
见How to get download progress in AFNetworking 2.0?
【讨论】:
【参考方案2】:创建自定义单元格 XIB 文件并在其上添加进度条
在您的视图控制器中开始下载,并访问进度条并在 didReceiveData 方法中设置进度
我刚刚在视图控制器中创建,您需要将它与表视图方法一起使用
代码示例:
@interface ViewController ()<NSURLConnectionDataDelegate>
//Put these in custom cell file
@property (weak, nonatomic) IBOutlet UIProgressView *progressView;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (strong, nonatomic) NSURLConnection *connectionManager;
@property (strong, nonatomic) NSMutableData *downloadedMutableData;
@property (strong, nonatomic) NSURLResponse *urlResponse;
@end
@implementation ViewController
- (void)viewDidLoad
[super viewDidLoad];
//Start download request logic
self.downloadedMutableData = [[NSMutableData alloc] init];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.planwallpaper.com/static/images/63906_1_BdhSun5.jpg"]
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:60.0];
self.connectionManager = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
#pragma mark - Delegate Methods
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
NSLog(@"%lld", response.expectedContentLength);
self.urlResponse = response;
//Show progress download while receiving data
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
[self.downloadedMutableData appendData:data];
self.progressView.progress = ((100.0/self.urlResponse.expectedContentLength)*self.downloadedMutableData.length)/100;
if (self.progressView.progress == 1)
self.progressView.hidden = YES;
else
self.progressView.hidden = NO;
NSLog(@"%.0f%%", ((100.0/self.urlResponse.expectedContentLength)*self.downloadedMutableData.length));
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
NSLog(@"Finished");
self.imageView.image = [UIImage imageWithData:self.downloadedMutableData];
结果看起来像
【讨论】:
如果我使用NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://drive.google.com/uc?export=download&id=0B6zMam2kAK39Zm5LUTctZXNLUlE»]
Progress 不起作用。为什么?
请参阅我们需要一个委托方法来获取下载进度 所以我使用了 NSURLConnection 类,因为它有 didReceiveData 我更新进度条的委托方法以上是关于如何在表格单元格中显示进度?的主要内容,如果未能解决你的问题,请参考以下文章