Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/105.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在表格单元格中显示进度?_Ios_Objective C_Uitableview_Cell - Fatal编程技术网

Ios 如何在表格单元格中显示进度?

Ios 如何在表格单元格中显示进度?,ios,objective-c,uitableview,cell,Ios,Objective C,Uitableview,Cell,我从服务器下载了一个文件。文件下载时,我想在表格单元格中显示进度百分比。我该怎么做 下载文件代码 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *filePath = [documentsDirectory string

我从服务器下载了一个文件。文件下载时,我想在表格单元格中显示进度百分比。我该怎么做

下载文件代码

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];
}

这在
[NSData dataWithContentsOfUrl]
中是不可能的。你需要一个图书馆,比如


请参阅
[NSData DATA with CONTENTS OFURL]
不可能实现的。你需要一个图书馆,比如


请参见创建自定义单元格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];
    }
@界面视图控制器()
//将这些放在自定义单元格文件中
@属性(弱、非原子)IBUIProgressView*progressView;
@属性(弱、非原子)IBUIImageView*imageView;
@属性(强,非原子)NSURLConnection*connectionManager;
@属性(强,非原子)NSMutableData*下载的MutableData;
@属性(强,非原子)NSURLSERPONSE*URLSERPONSE;
@结束
@实现视图控制器
-(无效)viewDidLoad
{
[超级视图下载];
//启动下载请求逻辑
self.downloademdutabledata=[[NSMutableData alloc]init];
NSURLRequest*urlRequest=[nsurlRequestRequestWithURL:[nsurlUrlWithString:@]https://www.planwallpaper.com/static/images/63906_1_BdhSun5.jpg"]
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:60.0];
self.connectionManager=[[NSURLConnection alloc]initWithRequest:urlRequest委托:self];
}
#pragma标记-委托方法
-(void)连接:(NSURLConnection*)连接DidReceiverResponse:(NSURResponse*)响应{
NSLog(@“%lld”,response.expectedContentLength);
self.urresponse=响应;
}
//接收数据时显示下载进度
-(void)连接:(NSURLConnection*)连接didReceiveData:(NSData*)数据{
[self.downloademdutabledata-appendData:data];
self.progressView.progress=((100.0/self.urresponse.expectedContentLength)*self.downloadedMutableData.length)/100;
if(self.progressView.progress==1){
self.progressView.hidden=是;
}否则{
self.progressView.hidden=否;
}
NSLog(@“%.0f%%”,((100.0/self.urresponse.expectedContentLength)*self.downloadedMutableData.length));
}
-(无效)连接IDFinishLoading:(NSURLConnection*)连接{
NSLog(@“完成”);
self.imageView.image=[UIImage imageWithData:self.downloadedMutableData];
}
结果看起来像


创建自定义单元格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];
    }
@界面视图控制器()
//将这些放在自定义单元格文件中
@属性(弱、非原子)IBUIProgressView*progressView;
@属性(弱、非原子)IBUIImageView*imageView;
@属性(强,非原子)NSURLConnection*connectionManager;
@属性(强,非原子)NSMutableData*下载的MutableData;
@属性(强,非原子)NSURLSERPONSE*URLSERPONSE;
@结束
@实现视图控制器
-(无效)viewDidLoad
{
[超级视图下载];
//启动下载请求逻辑
self.downloademdutabledata=[[NSMutableData alloc]init];
NSURLRequest*urlRequest=[nsurlRequestRequestWithURL:[nsurlUrlWithString:@]https://www.planwallpaper.com/static/images/63906_1_BdhSun5.jpg"]
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:60.0];
self.connectionManager=[[NSURLConnection alloc]initWithRequest:urlRequest委托:self];
}
#pragma标记-委托方法
-(void)连接:(NSURLConnection*)连接DidReceiverResponse:(NSURResponse*)响应{
NSLog(@“%lld”,response.expectedContentLength);
self.urresponse=响应;
}
//接收数据时显示下载进度
-(void)连接:(NSURLConnection*)连接didReceiveData:(NSData*)数据{
[self.downloademdutabledata-appendData:data];
self.progressView.progress=((100.0/self.urresponse.expectedContentLength)*self.downloadedMutableData.length)/100;
if(self.progressView.progress==1){
self.progressView.hidden=是;
}否则{
self.progressView.hidden=否;
}
NSLog(@“%.0f%%”,((100.0/self.urresponse.expectedContentLength)*self.downloadedMutableData.length));
}
-(无效)连接IDFinishLoading:(NSURLConnection*)连接{
NSLog(@“完成”);
self.imageView.image=[UIImage imageWithData:self.downloadedMutableData];
}
结果看起来像


如果我使用
NSURLRequest*urlRequest=[nsurlRequestRequestWithURL:[nsurlUrlWithString:@]https://drive.google.com/uc?export=download&id=0B6zMam2kAK39Zm5LUTctZXNLUlE»]
Progress不起作用。为什么?请看,我们需要一个委托方法来获取下载进度,所以我使用了NSURLConnection类,因为它具有didReceiveData委托方法,我在其中更新进度barif我使用
NSURLRequest*urlRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@]https://drive.google.com/uc?export=download&id=0B6zMam2kAK39Zm5LUTctZXNLUlE»]
进度不起作用。为什么?请看,我们需要一个委托方法来获取下载进度,所以我使用了NSURLConnection