2014-04-20 1 views
0

콜백을 사용하여 "cellForRowAtIndexPath"이미지를로드 할 때 UITableView에 Asynchoronous GCD를 사용하고 있습니다. 내를 tableView에서다중 GCD를 사용하여 TableView에서 이미지로드

, 각 행은 (평균, 각 행에 난 asynchoronous GCD를 사용하여 두 이미지를로드 할 수있는) 2 생성물을 -> 다음과 같이 여기

enter image description here

내 코드 로드 이미지

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    //Some code to init CustomCell 
    //Load Label1 
    //Load Label2 
    //Load image1 
    if(product1.thumImgData != nil){ 
     //Update image from datasource 
    } else{ 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ 
      NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:product1.thumImg]]; 
      if (imgData) { 
       UIImage *image = [UIImage imageWithData:imgData]; 
       if (image) { 
        dispatch_async(dispatch_get_main_queue(), ^{ 
         CustomViewProductByCategory *updateCell = (id)[self.tvProduct cellForRowAtIndexPath:indexPath]; 
         if (updateCell){ 
          product1.thumImgData = imgData; 
          updateCell.imgProduct1.image = image; 
          [updateCell.indicatorProduct1 stopAnimating]; 
         } 
        }); 
       } 
      } 

     }); 
    } 

    if(product2.thumImgData != nil){ 
     //Update image from datasource 
    } else{ 
    //load image2 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ 
      NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:product2.thumImg]]; 
      if (imgData) { 
       UIImage *image = [UIImage imageWithData:imgData]; 
       if (image) { 
        dispatch_async(dispatch_get_main_queue(), ^{ 
         CustomViewProductByCategory *updateCell = (id)[self.tvProduct cellForRowAtIndexPath:indexPath]; 
         if (updateCell){ 
          product2.thumImgData = imgData; 
          updateCell.imgProduct2.image = image; 
          [updateCell.indicatorProduct2 stopAnimating]; 
         } 
        }); 
       } 
      } 

     }); 
    } 

asynchoronous 내가 그렇게 때로는 cellForRowAtIndexPath 2 GCD를 사용하고의 때문에, 생각, 내가 어떤 이미지를로드 할 수 없습니다.

아무도이 문제를 해결하는 방법을 알고 있습니까? Tks 사전에.

+0

동일한 문제가 있었는데 이미지 데이터의 크기가 얼마나 큽니까? 테이블 뷰 셀에서 사용할 작은 축소판 이미지를 만드는 것이 가장 좋습니다. – DogCoffee

+0

서버에 작은 데이터를 보내달라고 요청했습니다. 그냥 80x80px –

답변

0

문제는 GCD를 사용하지 않기 때문에 문제는 세포가 그렇게 작동하지 않는다는 것입니다. 많은 행이있는 테이블 뷰가 있습니다. tableview 행을 사용하면 값이 싸고 셀은 저렴하지 않습니다. 따라서 iOS는 동일한 셀을 반복적으로 재사용합니다. 화면에서 볼 수있는만큼 많은 셀을 필요로하며 행만큼 많지 않습니다.

비동기 코드에서 이 아닌 셀을로드하고 이미지를 설정해보십시오. 대신 이미지를 데이터 모델에 저장하고 변경된 테이블 뷰의 행을 다시 그리도록 tableview에 지시하십시오.

+0

당신은 GCD를 사용하여 모든 이미지가로드되고 데이터 모델에 저장된다는 것을 의미합니다. 이 데이터 모델의 모든 이미지를로드 한 후 tableview를 다시 그립니다. –

+0

"image2"를로드하는 GCD에 대한 의견이 있으니 틀렸다고 생각합니다. 그리고 tableview는 잘 작동합니다! –