Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/98.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 更改cellForItemAtIndexPath中的图像:_Ios_Uicollectionview - Fatal编程技术网

Ios 更改cellForItemAtIndexPath中的图像:

Ios 更改cellForItemAtIndexPath中的图像:,ios,uicollectionview,Ios,Uicollectionview,我在这个cellForItemAtIndexPath:(NSIndexPath*)indexPath dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSURL *imageURL = [NSURL URLWithString:[[Listname objectAtIndex:indexPath.item]coverURL]]

我在这个
cellForItemAtIndexPath:(NSIndexPath*)indexPath

      dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{           
            NSURL *imageURL = [NSURL URLWithString:[[Listname objectAtIndex:indexPath.item]coverURL]];
            NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
            UIImage *image = [UIImage imageWithData:imageData];

            dispatch_async(dispatch_get_main_queue(), ^{

            shelfcell.bookCover.image = [UIImage imageWithData:imageData];


         });
     });
这个
shelfcell.bookCover.image
可以显示图像,但当我使用

if(_checkversionfromBookPathF==0.1){shelfcell.bookCover.alpha=0.5f;}

else如果(\u checkversionfromBookPathF==1){shelfcell.bookCover.alpha=1.0f;}

图像将仅显示
\u checkversionfromBookPathF==1
,但将首先显示此图像
\u checkversionfromBookPathF==0.1
,然后显示
\u checkversionfromBookPathF==1


为什么图像不去
shelfcell.bookCover.alpha=0.5f请任何人向我解释。

您不能直接使用“==”来比较浮点,而不能使用符号==。


重复使用同一个单元格可能会出现一些问题。由于您正在另一个线程中运行,因此加载映像将需要一些时间,在它完成加载映像之前,此单元格可能会被重新用于另一个执行shelfcell.bookCover.alpha=1.0f的indexPath

解决方案->

NSInteger row=indexPath.row;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{           
            NSURL *imageURL = [NSURL URLWithString:[[Listname objectAtIndex:indexPath.item]coverURL]];
            NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
            UIImage *image = [UIImage imageWithData:imageData];

            dispatch_async(dispatch_get_main_queue(), ^{
              //set image if below condition satisfies
              if([tableView indexPathForCell:cell] . row == row){
                  shelfcell.bookCover.image = [UIImage imageWithData:imageData];
               }
     });

您是说alpha未应用于图像?还是它没有进入内部(如果块的话)_checkversionfromBookPathF=.1?它将进入
shelfcell.bookCover.alpha=0.5f首先,但在加载完成后,它是这样的
shelfcell.bookCover.alpha=1.0f重用同一单元格可能会出现一些问题。因为您正在另一个线程中运行,所以加载映像需要一些时间,在它完成加载映像之前,此单元格可能会被重新用于另一个indexPath,其中执行shelfcell.bookCover.alpha=1.0f。哦,天哪,我不知道如何修复,但非常感谢,我会尝试。非常感谢我正在尝试此
f(_checkversionfromBookPathF==1.0f){if([collectionView indexPathForCell:cell].row==row){shelfcell.bookCover.image=[UIImage imageWithData:imageData];shelfcell.bookCover.alpha=0.5f;}
这很好,但是为什么我只模糊了一张图像,因为所有的
\u checkversionfromBookPathF=1
都有一张
\u checkversionfromBookPathF=1.1
请解释我。你不应该使用==