2014-03-19 1 views
0

테이블에서 스크롤 속도가 느려지고 웹에서로드되고 크기가 조정 된 이미지가 스크롤러에 있지만 이미지가 이미로드되어 스크롤이 왜 느려지는지 이해할 수 없습니다. 내가 읽고 성공하지 slow scrolling of UITableView을 시도했다 (내가 빈 셀을 참조)테이블보기에서 느린 스크롤

이 셀 인을로드 셀에 코드가없는

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


    static NSString *CellIdentifier = @"Cell"; 

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

    NSString *[email protected]""; 
    NSString *icon; 

      NSMutableArray *result=[allResults objectAtIndex:indexPath.section]; 
      NSDictionary *dic=[result objectAtIndex:indexPath.row+1]; 
      if([[result objectAtIndex:0] isEqualToString:@"types"]) 
      { 
       NSString *title=[dic objectForKey:@"title"]; 
       icon=[dic objectForKey:@"icon"]; 
       data=[data stringByAppendingString:[NSString stringWithFormat:@"%@",title]]; 
      } 
      if([[result objectAtIndex:0] isEqualToString:@"subServices"]) 
      { 
       NSString *title=[dic objectForKey:@"title"]; 
       icon=[dic objectForKey:@"icon"]; 
        data=[data stringByAppendingString:[NSString stringWithFormat:@"%@",title]]; 

      } 
      if([[result objectAtIndex:0] isEqualToString:@"businesses"]) 
      { 
       NSString *title=[dic objectForKey:@"title"]; 
       icon=[dic objectForKey:@"logo"]; 
        data=[data stringByAppendingString:[NSString stringWithFormat:@"%@",title]]; 

      } 

    cell.textLabel.numberOfLines = 1; 
    cell.textLabel.text = data; 
    cell.textLabel.textColor=[UIColor blackColor]; 
    cell.textLabel.font = [UIFont fontWithName:@"Arial Rounded MT Bold" size:22]; 
    cell.textLabel.textColor=[UIColor colorWithRed:122.0/255.0 green:181.0/255.0 blue:196.0/255.0 alpha:1]; 
    cell.imageView.layer.masksToBounds = YES; 


    //load image 
    NSURL *url = [NSURL URLWithString:icon]; 
    NSData *imdata = [NSData dataWithContentsOfURL:url]; 
    UIImage *logo=[UIImage imageWithData:imdata scale:1]; 

    UIImage *scaled=[self resizeImage:logo imageSize:CGSizeMake(30, 30)]; 
    cell.imageView.image=scaled ; 
    cell.imageView.layer.masksToBounds = YES; 
    cell.imageView.layer.cornerRadius = 12.0; 




    return cell; 
} 
+0

'else/if' (세 개의'if' 테스트 연속체에 대해)를 사용해야합니다. 게으른 로딩 (URL 이미지 로딩)을 볼 수도 있습니다. – Larme

답변

1

먼저 아이콘 URL은 무엇입니까? 이미 다운로드 한 로컬 이미지의 파일 URL입니까? 그렇지 않다면 백그라운드 스레드에서 다운로드하여 로컬 어딘가에 캐시하고 싶으면 여기에 로컬 파일 URL을 사용하십시오. 파일이 백그라운드 스레드에서 다운로드되면 전체 테이블을 다시로드하지 않으려합니다. 해당 이미지와 관련된 하나의 셀 (또는 더 나은 하나의 imageView)을 업로드하십시오. 그렇지 않으면 다른 문제를 일으킬 수있는 시간을 무수히 재로드하게됩니다.

다음으로 모든 통화마다 이미지의 크기가 조정됩니다. 이미지의 크기를 한 번 조정하고 그 결과를 캐시해야합니다. 이 한 위치에서만 이미지를 사용하는 경우 이미지를 다운로드 할 때 크기를 조정하고 크기가 조정 된 버전 만 캐시합니다. 다른보기에서 사용하는 경우 원본 및 크기 조정 된 버전을 캐시하십시오.

작은 일이지만 3면을 if/else ifs로 변경하십시오. 동일한 값을 확인 중이므로 세 번 확인하지 않아도됩니다. 가장 인기있는 옵션이 먼저 선택되도록 순서를 변경하면 몇 가지 비교를 저장할 수 있습니다.

는 업데이트 :

이 더 빠르게 할 수있는 또 다른 것은, 한 번 셀을 구성 할 수 있습니다. 글꼴, 색상 등을 매번 설정합니다. 이것을 셀의 서브 클래스 init 메소드로 옮기면, 그것을 반복해서 호출 할 필요가 없습니다.

또한 수행 할 일이 몇 가지 있습니다. 몇 가지 메모와 함께이 업데이트 된 버전을 확인합니다

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

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


    // Move this part to the init method of a subclass of UITableViewCell 
    cell.textLabel.numberOfLines = 1; 
    cell.textLabel.textColor = [UIColor blackColor]; 
    cell.textLabel.font = [UIFont fontWithName:@"Arial Rounded MT Bold" 
              size:22]; 
    cell.textLabel.textColor = [UIColor colorWithRed:122.0/255.0 
               green:181.0/255.0 
               blue:196.0/255.0 
               alpha:1]; 
    cell.imageView.layer.masksToBounds = YES; 
    cell.imageView.layer.cornerRadius = 12.0; 
    // End of section to move to init methods 

    NSString* icon = nil; 

    NSMutableArray* result = [allResults objectAtIndex:indexPath.section]; 
    NSDictionary* dic = [result objectAtIndex:indexPath.row + 1]; 
    if ([[result objectAtIndex:0] isEqualToString:@"types"]) { 
     icon = [dic objectForKey:@"icon"]; 
    } 
    else if ([[result objectAtIndex:0] isEqualToString:@"subServices"]) { 
     icon = [dic objectForKey:@"icon"]; 
    } 
    else if ([[result objectAtIndex:0] isEqualToString:@"businesses"]) { 
     icon = [dic objectForKey:@"logo"]; 
    } 

    cell.textLabel.text = [dic objectForKey:@"title"]; 

    // Move the loading of the URL to a background thread if the url is not a local file URL 
    //load image 
    NSURL* url = [NSURL URLWithString:icon]; 
    NSData* imdata = [NSData dataWithContentsOfURL:url]; 
    UIImage* logo = [UIImage imageWithData:imdata 
            scale:1]; 

    // Move the resizing of the image to however you load the image from the network, 
    // resize it once and cache the results, load the cached version only 
    UIImage* scaled = [self resizeImage:logo 
           imageSize:CGSizeMake(30, 30)]; 
    cell.imageView.image = scaled; 

    return cell; 
} 
+0

나는 그것을 이해하지 못한다.이 방법은 한 번 호출된다. 시간이 걸리더라도 모든 이미지가 이미 사용 가능해야한다. 그래서 왜 계속 천천히 머물러야 하는가? 이미지를 반복해서로드합니까? 이 함수는 한 번 호출되지 않습니까? – Curnelious

+0

아니요,이 메서드는 셀이 화면에 나타날 때마다 호출됩니다. 스크롤 오프하여 다시 켜면 다시 호출됩니다. –

1

(그것은 또한 코드 섹션의 제목을 가지고) 이미지, 속도가 느려집니다. 일반적으로 이미지 다운로드를 사전 처리하여 셀 생성 방법에서 즉시 사용할 수 있도록하거나 셀 드로잉을지지하지 않도록 배경 스레드에 이미지를로드하도록 셀의 코드를 변경합니다.

사과에서 테이블보기 프로그래밍 가이드와 블록 프로그래밍 가이드를 확인하십시오. 간단한 답변을 드려 죄송합니다. 내 휴대 전화에 입력하고 있습니다.

1

으로는 동 기적으로 이미지를 다운로드하고,이 원인 느린 스크롤 그의 대답에 @Andrey Chernukha을 보여줍니다.

비동기 모드에서 이미지를로드하거나 동일한 기능을 가지고 SDWebImage를 사용 UIImageView에 큰 카테고리를을 AFNetworking, 또는 내용을로드하도록 NSURLSession 구현을 작성할 수 있습니다 사용할 수있는이 문제를 방지합니다.

관련 문제