Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/39.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.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
Iphone 更新UICollectionViewCell中的UIProgressView以下载文件_Iphone_Objective C_Ios_Uiprogressview - Fatal编程技术网

Iphone 更新UICollectionViewCell中的UIProgressView以下载文件

Iphone 更新UICollectionViewCell中的UIProgressView以下载文件,iphone,objective-c,ios,uiprogressview,Iphone,Objective C,Ios,Uiprogressview,下载文件时,我试图在UICollectionViewCell中更新UIProgressView,但有时progressView更新,有时不更新,我不明白为什么,这是显示UICollectionViewCell的代码: -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { static NSString

下载文件时,我试图在UICollectionViewCell中更新UIProgressView,但有时progressView更新,有时不更新,我不明白为什么,这是显示UICollectionViewCell的代码:

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

static NSString *cellIdentifier = @"documentCell";

UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];

NSManagedObject *doc = [self.magDocument objectAtIndex:indexPath.row];

 UIButton *btn = (UIButton *)[cell viewWithTag:2003];
[btn addTarget:self action:@selector(addDocument:) forControlEvents:UIControlEventTouchUpInside];
UIProgressView *progressBar = (UIProgressView *)[cell viewWithTag:2005];

return cell;
}
这是开始下载的按钮:

- (IBAction)addDocument:(id)sender
{
self.directCellPath = [self.myCollectionView indexPathForCell:(UICollectionViewCell *)[[sender superview] superview]];

NSManagedObject *document = [self.magDocument objectAtIndex:self.directCellPath.row];

[self loadLink:[document valueForKey:@"docUrl"]];
}

- (void)loadLink:(NSString *)urlDownload
{
UICollectionViewCell *cell = (UICollectionViewCell *)[self.myCollectionView cellForItemAtIndexPath:self.directCellPath];
UIProgressView *prg = (UIProgressView *)[cell viewWithTag:2005];

AFDownloadRequestOperation *request = [[AFDownloadRequestOperation alloc] initWithRequest:requestUrl targetPath:zipDownloadPath shouldResume:YES];

    [request setProgressiveDownloadProgressBlock:^(NSInteger bytesRead, long long totalBytesRead, long long totalBytesExpected, long long totalBytesReadForFile, long long totalBytesExpectedToReadForFile) {

            NSLog(@"%f",totalBytesReadForFile/(float)totalBytesExpectedToReadForFile);
        NSArray *progress = [NSArray arrayWithObjects:prg,[NSNumber numberWithFloat:totalBytesReadForFile/(float)totalBytesExpectedToReadForFile], nil];

        [self performSelectorOnMainThread:@selector(updateProgressBar:) withObject:progress waitUntilDone:NO];
    }];

    [self.downloadQueue addOperation:request];
 }

- (void)updateProgressBar:(NSArray *)progress
{
UIProgressView *pgr = (UIProgressView *)[progress objectAtIndex:0];
[pgr setProgress:[[progress objectAtIndex:1] floatValue]];
}

一次进度视图工作,另一次一千次不工作,我不明白如何更新进度视图,有什么帮助吗?

可能是关于线程问题。试试看

dispatch_async(dispatch_get_main_queue(), ^{
            UIProgressView *pgr = (UIProgressView *)[progress objectAtIndex:0];
            [pgr setProgress:[[progress objectAtIndex:1] floatValue]];
       });//end block

尝试异步或同步创建customCell并在其中显示所有可见的UIView(初始化、添加子视图…)。然后使用cellForItemAtIndexPath中的自定义方法激活(显示)它。如果您考虑MVC,cellForItemAtIndexPath只用于控制器,而不是视图

在您的情况下,将所有iAction、loadLink和updateProgress带到customCell并发送参数以激活它们,或者您可以创建像CustomCellDelegate这样的新协议来进行通信

查看更多信息:


非常感谢您的回答,首先我接受您的回答,您能否创建一个示例代码来解释解决方案,一个简单的uicollection视图和uicollectioviewcell子类,并插入一个uiimageview,从url加载图像并将其显示在uicollectionviewcell中,这样我就可以了解如何使用该类了,如果您执行以下示例代码,我将不胜感激:(非常感谢!所以我必须在uicollectionviewcell中添加下载代码?如果我想要一个处理所有下载的类来处理队列,那么我如何才能将进度条更新发送到uicollectionviewcell子类?您好,@Sang看看这个问题,我已经为您的请求编写了代码,但没有工作:(救命!