2012-02-21 4 views
1

서버에서 가져온 NSData의 이미지를로드하고 사용자 지정 UITableViewCell에 표시하려고하는데 올바르게 작동하지 않고 이미지를 반복해서 반복하고 있습니다. 상단 NSData의 이미지가 올바르게로드되지 않음

Table View

우리는 거기에서 올라가고, indexPath.row = 0 있습니다. 셀이로드되면 다음 코드를 호출하여 이미지를로드합니다 (NSLog를 사용할 때 모든 이미지에 올바른 URL이 반환 됨) 올바른 이미지를 다운로드하는 것처럼 보입니다. 하지만합니다 ( NSData 서버에서 반환

- (void) startLoadingImage 
{ 
    NSString *url = @"myurlhere"; 
    url = [url stringByAppendingFormat:identifier]; 
    NSLog(@"Image URL: %@", url); //THIS IS WHERE I DETERMINE THAT IT IS DOWNLOADING THE CORRECT IMAGE 
    NSURLRequest * request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:url]]; 
    NSURLConnection * imageConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
    [imageConnection start]; 
} 

는 셀은 네 개의 이미지를로드까지 잘 작동

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    if (!rawData) rawData=[[NSMutableData alloc]initWithLength:0]; 
    [rawData appendData:data]; 
} 
-(void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    NSLog(@"Image did finish loading. Displaying..."); 

    [productImage setImage:[[UIImage alloc]initWithData:rawData]]; 
} 

위임 방법을 ... 사용하고, 그것은 이미지를 반복 시작 이름과 가격 같은 셀의 정보가 올바르게 표시됩니다). 즉, 네 장마다 청바지 사진을 계속 볼 수 있습니다.

여기 내 결과는 NSLog입니다. 다시 말하지만 모든 것이 정확하다고 보입니다. 보시

2012-02-21 14:45:01.152 APPNAME[7873:11f03] Image URL: http://www.MYAPPURL.net/production/img/[email protected] 
2012-02-21 14:45:01.173 APPNAME[7873:11f03] Image URL: http://www.MYAPPURL.net/production/img/[email protected] 
2012-02-21 14:45:01.199 APPNAME[7873:11f03] Image did finish loading. Displaying... 
2012-02-21 14:45:01.205 APPNAME[7873:11f03] Image did finish loading. Displaying... 
2012-02-21 14:45:11.064 APPNAME[7873:11f03] Image URL: http://www.MYAPPURL.net/production/img/[email protected] 
2012-02-21 14:45:11.476 APPNAME[7873:11f03] Image did finish loading. Displaying... 
2012-02-21 14:45:12.432 APPNAME[7873:11f03] Image URL: http://www.MYAPPURL.net/production/img/[email protected] 
2012-02-21 14:45:12.751 APPNAME[7873:11f03] Image URL: http://www.MYAPPURL.net/production/img/[email protected] 
2012-02-21 14:45:13.010 APPNAME[7873:11f03] Image did finish loading. Displaying... 
2012-02-21 14:45:13.013 APPNAME[7873:11f03] Image did finish loading. Displaying... 
2012-02-21 14:45:14.574 APPNAME[7873:11f03] Image URL: http://www.MYAPPURL.net/production/img/[email protected] 
2012-02-21 14:45:14.948 APPNAME[7873:11f03] Image did finish loading. Displaying... 

그래서, 이미지 84, 85, 86, 89, 80, 81 로딩하지만, 단지 이미지 84, 85, 86, 89을 표시하고, 그런 다음이를 반복한다.

왜 이런 일이 일어나는 이유는 무엇입니까?

+1

마치 셀 재사용 문제처럼 보입니다. –

+0

어떻게 수정하겠습니까? – Baub

답변

4

UITableView은 (dequeueReusableCellWithIdentifier: 메서드를 통해) 사용자가 만든 UITableViewCell 개체를 다시 사용합니다. UITableViewCell 개체를 직접 수정하면 안됩니다. 대신 테이블 데이터를 다시로드 한 다음 tableView:cellForRowAtIndexPath: 대리자 메서드의 셀을 업데이트하십시오. 뷰 컨트롤러에서

, 당신이 뭔가를 할 것이다 : 당신의 대리자 메서드 호출하는 테이블 셀을 새로 고쳐집니다

 
- (void) connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    // set the product.image from the response data here 

    // reload table 
    [self.tableView reloadData]; 
} 

다시로드 데이터 : 모두를 보지 않고

 
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *cellIdentifier = @"CellId"; 

    UICustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
    if (cell == nil) 
    { 
     // create custom cell 
     cell = [[[UICustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease]; 
    } 

    Product *product = [products objectAtIndex:indexPath.row]; 

    cell.textLabel.text = product.name; 
    cell.image = product.image; 

    return cell; 
} 
+0

실제로 connectionDidFinishLoading에서 이미지로드가 완료된 셀의 인덱스 경로를 알고있는 경우 해당 인덱스 경로에서 셀을 다시로드하면됩니다. –

0

당신의 코드, 비동기 연결로 처리하는 데 문제가있을 수 있습니다. 특정 셀을 연결 요청에 바인딩하는 방법을 알지 못합니다.

Ray Wenderlich's Multithreading and Grand Central Dispatch on iOS for Beginners Tutorial의 예는 내가하는 것처럼 보이기에 아주 적합하다고 생각할 수 있습니다. 나는이 예를주의 깊게 살펴보고 그것이 당신을 위해 일할 수 있는지 알아보기를 권합니다. 당신은 sampel 프로젝트 (그 페이지의 하단에있는 링크)를 다운로드하고 그것이 어떻게 작동하는지 볼 수 있습니다.

관련 문제