2014-01-14 146 views
1

我對這個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 if(_checkversionfromBookPathF == 1) { shelfcell.bookCover.alpha = 1.0f; }

圖像將只顯示_checkversionfromBookPathF == 1但會首先顯示這一個_checkversionfromBookPathF == 0.1然後_checkversionfromBookPathF == 1

爲什麼圖像不去shelfcell.bookCover.alpha = 0.5f;請任何人解釋我。

+1

你是saing alpha不適用於圖像?或它不會進入(如果塊)_checkversionfromBookPathF ==。1? – santhu

+0

這將會'shelfcell.bookCover.alpha = 0.5f;'首先,但加載完成後它會像這樣'shelfcell.bookCover.alpha = 1.0f;' –

+1

可能會有一些問題重複使用相同的單元格。由於您正在另一個線程中運行,因此在加載映像之前需要一段時間才能加載映像,因此此單元可能會重新用於執行shelfcell.bookCover.alpha = 1.0f的另一個indexPath。 – santhu

回答

2

你不能直接使用'=='比較浮點數,使用<或>符號不是==。

重複使用同一個單元可能存在一些問題。由於您正在另一個線程中運行,因此在加載映像之前需要一些時間才能加載映像,因此此單元可能會重新用於執行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]; 
       } 
    }); 
+0

非常感謝你我試試這個'f(_checkversionfromBookPathF == 1.0f){if([collectionView indexPathForCell:cell]。row == row){shelfcell.bookCover.image = [UIImage imageWithData:imageData]; shelfcell.bookCover.alpha = 0.5f;}}'這是非常好的,但爲什麼我只有一個圖像模糊,因爲所有'_checkversionfromBookPathF = 1',並有一個'_checkversionfromBookPathF = 1.1'請解釋我。 –

+0

你不應該使用==比較浮點數 – santhu