2013-03-30 2 views
0

작은 텍스트가있는 이미지 테이블이 있습니다. 작동 방식 : Instagram에서 JSON 응답을 받아 모든 사전에 배열하는 모델이 있습니다. 내 메소드 cellForRowAtIndexPath에서 indexpath.row에 셀 태그를 설정 한 다음 dispatch_async를 사용하여 이미지를 다운로드하기 시작합니다 (모델에서 URL 가져 오기). 이미지로드가 끝나면 현재 셀과 현재 indexpath.row의 태그를 비교할 수있는 체크를하고, 같은 경우 이미지를 그립니다. 내 모델을 새로 고칠 때까지 제대로 작동합니다. 동일한 데이터가 다시로드되면 표에서 이상한 동작이 발생합니다. 처음 세 개의 셀이 동일한 이미지로 표시됩니다. 어떻게 해결할 수 있습니까?업데이트 UITableView - 잘못된 셀 및 동일한 이미지

여기 내 휴대 방법이다 : 나는 사방 [의 tableview reloadData] 퍼팅 시도했다

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 
    cell.tag = indexPath.row; 

    if (self.loader.parsedData[indexPath.row] != nil) 
    { 
     cell.imageView.image = nil; 

     dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul); 
      dispatch_async(queue, ^(void) { 

       NSString *url = [self.loader.parsedData[indexPath.row] objectForKey:@"imageLR"]; 

       if ([self.cache objectForKey:url] != nil) 
       { 
        NSData *imageData = [self.cache objectForKey:url]; 
        self.tempImage = [[UIImage alloc] initWithData:imageData]; 
       } 

       else 
       { 

       NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]]; 
       self.tempImage = [[UIImage alloc] initWithData:imageData]; 
       [self.cache setObject:imageData forKey:[self.loader.parsedData[indexPath.row] objectForKey:@"imageLR"]]; 

       } 
       dispatch_async(dispatch_get_main_queue(), ^{ 
        if (cell.tag == indexPath.row) 
        { 
         cell.imageView.image = self.tempImage; 
         [cell setNeedsLayout]; 
        } 

        }); 
      }); 

    cell.textLabel.text = [self.loader.parsedData[indexPath.row] objectForKey:@"id"]; 
    } 

    return cell; 
} 

하지만, 도움이되지 않습니다.

답변

0

문제점을 발견했습니다. 다운로드 한 데이터를 보관하기 위해 tableViewController에 property를 사용했습니다. 그래서 언제든지 다양한 블록이 동시에 그 블록에 쓸 수 있으며 이미지를 설정하기 위해 해당 포인터를 사용할 수 있습니다. 업데이트에서 어떤 일이 발생했는지, 여러 블록에서 동시에 사용하여 동일한 이미지가 생성되었습니다. 로컬 NSData 변수를 사용하도록 전환했으며 현재 잘 작동합니다.

관련 문제