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
请解释我。你不应该使用==