2011-11-30 2 views
5

테두리 색을 추가하고 그림자를 내 UITableViewCell 배경에 추가하는 코드는 다음과 같습니다. 내 문제는이 코드는 tableView 자체에 큰 지연을 일으키는 것입니다.그림자 및 테두리로 인한 UITableView 뒤틀림

내 코드를 최적화하여 UITableView의 지연을 방지하는 방법을 알려주시겠습니까?

if ([cell viewWithTag:012] == nil && comment.isReply == NO) { 
    UIImageView *iv = [[[UIImageView alloc] initWithFrame:frame] autorelease]; 
    [iv setImage:[UIImage imageNamed:@"paper"]]; 
    [iv setTag:012]; 
    [cell insertSubview:iv atIndex:0]; 

    [iv.layer setBorderWidth:1.0]; 
    [iv.layer setBorderColor:[[UIColor whiteColor] CGColor]]; 

    [iv.layer setShadowColor:[[UIColor blackColor] CGColor]]; 
    [iv.layer setShadowOffset:CGSizeMake(0, 1)]; 
    [iv.layer setShadowOpacity:0.75]; 

} 

else if ([cell viewWithTag:012] == nil && comment.isReply == YES) { 

    frame.origin.x += 35; 

    UIImageView *iv = [[[UIImageView alloc] initWithFrame:frame] autorelease]; 
    [iv setImage:[UIImage imageNamed:@"paper"]]; 
    [iv setTag:012]; 
    [cell insertSubview:iv atIndex:0]; 

    UIImage *arrow = [UIImage imageNamed:@"arrow"]; 
    UIImageView *ivs = [[[UIImageView alloc] initWithFrame:CGRectMake(-12, ([cell frame].size.width/2) + ([arrow size].width/2) , arrow.size.width, arrow.size.height)] autorelease]; 
    [cell addSubview:ivs]; 

    [iv.layer setBorderWidth:1.0]; 
    [iv.layer setBorderColor:[[UIColor whiteColor] CGColor]]; 

    [iv.layer setShadowColor:[[UIColor blackColor] CGColor]]; 
    [iv.layer setShadowOffset:CGSizeMake(0, 0)]; 
    [iv.layer setShadowOpacity:0.75]; 

} 
+0

정확하게 그림자를 추가 하시겠습니까? UITableView 데이터 소스에서 그렇게 할 수 있습니까? 그렇다면 실제로 사용자 정의 UITableViewCell 서브 클래스를 만들고 거기에서 사용자 정의 UI를 수행해야합니다. –

+0

죄송합니다. cellForRowAtIndexPath 메소드에 해당 사항이 없습니다. –

답변

1

각로드에서 셀을 조작하지 말고 대신 셀을 초기화/생성 할 때 UI를 조정해야합니다.

설명하기 위해 cellForRowAtIndexPath: 메서드를 사용하여 새 셀 또는 여러 셀을로드 할 때마다 현재이 메서드에서 많은 뷰 변경 작업을 수행하고 있지만 이것이 필요하지 않은 경우가있을 수 있습니다 (예 : 예를 들어 새로운 셀은 방금 화면에서 스크롤 한 것과 같은 유형입니다. 이 UI 수정을 셀이 초기화되는 위치로 이동합니다 (데이터가 바뀌지 않은 곳). 서브 클래스를 사용하거나 간단히이 작업을 수행 할 수 있습니다.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    // Reuse id 
    static NSString *identifier1 = @"identifer-1"; 
    static NSString *identifier2 = @"identifier-2"; 
    static NSString *regular = @"regular"; 

    UITableViewCell *cell; 

    if (comment.isReply == NO) { 
     cell = [tableView dequeueReusableCellWithIdentifier: identifier1]; 

     if (cell == nil) { 
      cell = [[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: identifier1] autorelease]; 

      // Do the UI modification here 
     } 
    } else if (comment.isReply == YES) { 
     cell = [tableView dequeueReusableCellWithIdentifier: identifier2]; 

     if (cell == nil) { 
      cell = [[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: identifier2] autorelease]; 

      // Do the UI modification here 
     } 
    } else { 
     // Regular cell 
     cell = [tableView dequeueReusableCellWithIdentifier: regular]; 

     if (cell == nil) { 
      cell = [[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: regular] autorelease]; 
     } 
    } 

    // Load the data into the cell 

    return cell; 
} 

는 내가이와 함께 갈거야 어디서 희망, 키는 가능한 한 적은 무거운 물건을하고 jQuery과 캐싱이 더 큰 영향을 미칠 수 있도록하는 것입니다.

20

다른 최적화 조언 외에도 CALayershadowPath을 지정하면 그림자 렌더링 성능이 향상됩니다. 당신은

iv.layer.shadowPath = [UIBezierPath bezierPathWithRect:iv.bounds].CGPath; 

또한의 CALayer에 shouldRasterize 비트를 조사 할 수 있습니다 ...이 같은 뭔가 그림자에 대한 경로를 결정할 수있다. 그러면 레이어가 비트 맵으로 미리 렌더링됩니다. 이 경로를 선택하면 장치와 일치하는 래스터 화 스케일도 제공해야합니다.

cell.layer.shouldRasterize = YES; 
cell.layer.rasterizationScale = [UIScreen mainScreen].scale; 
+1

몇 가지 다른 shadowPath를 사용하여 위대한 [블로그 게시물] (http://nachbaur.com/blog/fun-shadow-effects-using-custom-calayer-shadowpaths)을 확인하십시오. –

+1

이 래스터 화에 대한 조언에 많은 감사를드립니다. ! 나를 위해 트릭을;) 지금 내 테이블 뷰 스크롤 너무 부드럽게 ^^ – polo987

+0

고맙습니다, 고맙습니다! 미안 해요, 한 번만 투표 할 수 있어요. –