Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/28.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
Objective c AFHTTPRequestOperation在视图关闭并重新打开时还原进度条?_Objective C_Uicollectionview_Afnetworking_Nsoperation - Fatal编程技术网

Objective c AFHTTPRequestOperation在视图关闭并重新打开时还原进度条?

Objective c AFHTTPRequestOperation在视图关闭并重新打开时还原进度条?,objective-c,uicollectionview,afnetworking,nsoperation,Objective C,Uicollectionview,Afnetworking,Nsoperation,目前,我的应用程序存在一个问题,用户退出视图控制器后,我如何在UICollectionViewCell中恢复活动的UIProgressView,方法是单击“上一步”,然后返回视图控制器 当用户退出视图时,AFHTTPRequestOperation仍在后台运行,正在下载文件,但是返回后,看起来下载没有进行,UIProgressView返回到0 这是我在单击Collection View单元格上的download之后运行的代码,它工作正常,但只适用于视图的生命周期 [operation1 set

目前,我的应用程序存在一个问题,用户退出视图控制器后,我如何在UICollectionViewCell中恢复活动的UIProgressView,方法是单击“上一步”,然后返回视图控制器

当用户退出视图时,AFHTTPRequestOperation仍在后台运行,正在下载文件,但是返回后,看起来下载没有进行,UIProgressView返回到0

这是我在单击Collection View单元格上的download之后运行的代码,它工作正常,但只适用于视图的生命周期

 [operation1 setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
        NSLog(@"Progress = %f", (float)totalBytesRead / totalBytesExpectedToRead );
        float progressValue = (float)totalBytesRead / totalBytesExpectedToRead;
        [cell.downloadProgress setProgress:progressValue];

    }];

    [operation1 start];
有关如何在重新加载收藏视图时恢复该下载状态/进度的提示


谢谢Aaron

如果向下滚动并备份收藏视图,这可能也不起作用。问题是
单元格
在滚动到屏幕外之前只指向正确的单元格(它可能会被重新用于不同的内容,或者以后可能是
nil

操作应该告诉控制器进度,而不是单元格

一种可能的实现如下所示:

__weak typeof(operation1)weakOperation = operation1;
[operation1 setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
    __strong __typeof(weakOperation)strongOperation = weakOperation;
    NSLog(@"Progress = %f", (float)totalBytesRead / totalBytesExpectedToRead );
    float progressValue = (float)totalBytesRead / totalBytesExpectedToRead;
    [self updateProgress:progressValue forOperation:strongOperation];
}];

[operation1 start];
- (void) updateProgress:(float)progress forOperation:(AFHTTPRequestOperation *)operation {
    YourCustomCollectionViewCell *cell = // find the correct cell using 'operation', and your data model
    [cell.downloadProgress setProgress:progress];
}
控制器中的方法可能如下所示:

__weak typeof(operation1)weakOperation = operation1;
[operation1 setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
    __strong __typeof(weakOperation)strongOperation = weakOperation;
    NSLog(@"Progress = %f", (float)totalBytesRead / totalBytesExpectedToRead );
    float progressValue = (float)totalBytesRead / totalBytesExpectedToRead;
    [self updateProgress:progressValue forOperation:strongOperation];
}];

[operation1 start];
- (void) updateProgress:(float)progress forOperation:(AFHTTPRequestOperation *)operation {
    YourCustomCollectionViewCell *cell = // find the correct cell using 'operation', and your data model
    [cell.downloadProgress setProgress:progress];
}

您好,谢谢您的回复,我引用了IndexPath,所以当您滚动时它只出现了一次,您的解决方案看起来不错,我一直在思考这些问题,但无法确定如何找到正确的sell using操作?我要多读些书。再次感谢您可以保留对数据模型中所有操作的引用,或者您可以尝试根据url确定哪个单元格是正确的(查看
operation.request
)。正确的方法取决于您具体执行的操作。谢谢,进一步查看updateProgress方法在视图关闭时仍然运行的代码,并引用旧的IndexPath,因此我想我需要弄清楚如何在视图再次打开时传递正确的单元格/IndexPath。索引路径是否始终相同?如果不是,它取决于什么?是的,如果indexath.row=0,则indexath会更改我在collectionView cellForItemAtIndexPath中作为基本测试所做的操作。我将indexath存储在属性中_downloadingIndexPath=indexath;这在updateProgress中使用,它工作正常,直到视图关闭并再次打开。看起来IndexPath已更改,并且操作未获得最新的下载IndexPath值。再次感谢