2013-03-27 3 views
2

서버에서 내 셀의 데이터를 가져올 때 테이블 뷰를 보는 데 문제가 있습니다. 사진을 사용하지 않으면 스크롤이 끊어지지 않지만 이미지도 사용하고 싶습니다. 아무도 내가 이것을 해결할 수있는 방법을 알고 있습니까? 내 서버의 plist에서 데이터를 가져오고 있습니다. 여기 uitableview에서 스크롤이 끊어짐

는 스크롤 휴식을 만드는 이미지의 코드

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 


{ 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 




    if (cell == nil) { 

     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 

    } 


    NSURL *URL = [[NSURL alloc] initWithString:[[self.content objectAtIndex:indexPath.row] valueForKey:@"imageName"]]; 

    NSData *URLData = [[NSData alloc] initWithContentsOfURL:URL]; 



    UIImage *img = [[UIImage alloc] initWithData:URLData]; 

    UIImageView *imgView = (UIImageView *)[cell viewWithTag:100]; 

    imgView.image = img; 

.... 
+0

는 "휴식"무엇을 의미합니까 :

는 여기에 몇 가지 예제 코드인가? –

+0

스크롤을 아래로 스크롤 할 때 작은 휴식을 한 다음 스크롤합니다. 내가 이미지에 댓글을 달았고 일어나지 않는 이미지가 없다면. –

+0

@JimmyLee의 답변 확인 –

답변

2

(내가 정의 스타일 셀을 사용하고) 당신은 스크롤 정지와 ​​시작은,이 이미지가로드되어 있기 때문에 경우가 될 수 있음을 의미하는 경우 서버에서 (상당한 시간이 걸릴 수 있음) 주 스레드에서 실행하면 동결이 발생합니다.

이 경우 수정 프로그램은 다른 스레드에서 이미지를 가져 오는 것입니다. 다행스럽게도 iOS에는 Grand Central Dispatch라는 멀티 스레딩 시스템을 사용하기가 쉽습니다.

dispatch_queue_t q = dispatch_queue_create("FetchImage", NULL); 
    dispatch_async(q, ^{ 
     /* Fetch the image from the server... */ 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      /* This is the main thread again, where we set the tableView's image to 
       be what we just fetched. */ 
     }); 
    }); 
+0

코드에서 어떻게 구현할 수 있습니까? 나는 다른 방법을 시도했지만 어쩌면 내가 뭔가 잘못하고있다. –

+0

NSURL, NSData, UIImage를'/ * Fetch the image ...'부분에 넣고 실제로 셀의 그림을'/ * '로 설정하는 코드를 넣는 것이 좋습니다. 메인 스레드 ...'부분. 이 모든 것은'tableView : cellForRowAtIndexPath :'메소드 내부에 있어야한다. –

+0

나는 이것을 넣어서 일하지 않았다. 가져 오기에서 NSURL, NSDARA 및 UIImage ... 그리고 UIImageView 및 imgView.image가 메인입니다. –