2012-09-11 3 views
2

UITableView가있는 UIViewController가 매우 느립니다 (당밀로 느림) 아마도 그룹화 된 UITableview의 UITableViewCells에 대한 사용자 정의 배경 이미지 설정 때문일 수 있습니다. ViewController가 소유합니다. Cocoa With Love Custom UITableView Drawing사용자 정의 UITableViewCells (깨진 레코드)를 사용하여 ViewController로드 속도 향상

: 나는 또한 내가 어리석은 일을하고 있지 않다 확인하기 위해이 문서를 따랐습니다 Tricks for improving iPhone UITableView scrolling performance

:

나는 추천이 다른 SO 질문에 명시된 바와 같이 나는만큼 다음과 같아

배경 스타일 코드를 호출하지 않는 즉시 성능이 향상됩니다.

제안에 따라, 내가 뭘 모든 단계 : 때문에 디자인이 방법의

  • , 나는 2 개의 다른 세포 변화가 균일 한 행 높이를 가질 수 없습니다. 셀 변형에 관계없이 행 배경은 항상 3 개의 이미지 중 1 개이며 행 인덱스를 기반으로합니다. 0 : top_row_image, n-1 : bottom_row_image, 기타 모두 : middle_row_image
  • 내 ViewController의 viewDidLoad에서 배경에 사용 된 이미지를로드하고 있습니다. 내가
  • 세포의 층이
  • 이 뾰족 셀을로드하지 재사용되고 불투명
  • 셀 식별자로 설정되어 있는지 셀 핸들 내부의 UITextField를시키는거야으로
  • 나는 어떤의 drawRect 코드가없는

는 기본적으로, 다른 상단되는 행의 유형은 그것이 입력 따라 중간과 맨 아래 행의 배경 이미지와 테이블의 섹션의 행의 각 스타일을 원하는 파일. 누구든지 UITableView에 사용자 지정 배경을 가지고 더 나은 방법을 제안 할 수 있습니까? 난 당신이 cellForRowAtIndexPath에 모든 호출에 모든 단일 셀의 배경 이미지를 전환 중지 경우 상당한 성능 향상을 볼 수있을 거라 생각

ViewController: 
@property (nonatomic, weak) IBOutlet       *tableview; 
@property (nonatomic, strong, readwrite) UIImage    *topRowImage; 
@property (nonatomic, strong, readwrite) UIImage    *middleRowImage; 
@property (nonatomic, strong, readwrite) UIImage    *bottomRowImage; 

@synthesize tableview = _tableview;  
@synthesize topRowImage = _topRowImage;   
@synthesize middleRowImage = _middleRowImage; 
@synthesize bottomRowImage = _bottomRowImage; 

// Load the images that will be used for the cell background ahead of time 
- (void)viewDidLoad 
{ 
    [super viewDidLoad];   

    // All of the images are 60x20 but my cells are 300x44 or 300x56 
    UIEdgeInsets edgeInsets = UIEdgeInsetsMake(2, 4, 2, 4);   
    self.topRowImage = [[UIImage imageNamed:@"top_row.png"] resizableImageWithCapInsets:edgeInsets];   

    UIEdgeInsets edgeInsets = UIEdgeInsetsMake(0, 4, 0, 4); 
    self.middleRowImage = [[UIImage imageNamed:@"middle_row.png"] resizableImageWithCapInsets:edgeInsets];    

    edgeInsets = UIEdgeInsetsMake(2, 4, 2, 4); 
    self.bottomRowImage = [[UIImage imageNamed:@"bottom_row.png"] resizableImageWithCapInsets:edgeInsets];  
} 

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *cellId = [self getCellIdAt:indexPath]; 

    BaseCustomTableViewCell *cell = (BaseCustomTableViewCell *)[aTableView dequeueReusableCellWithIdentifier:cellId]; 

    if (cell == nil) 
    { 
     if ([cellId isEqualToString:@"CellId1"]) 
     { 
      cell = [[CustomTableViewCell1 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];  
     } 
     else 
     { 
      cell = [[CustomTableViewCell2 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];  
     } 
    } 

    // the following line seems to be the bottleneck 
    [self styleBackground:cell indexPath:indexPath totalRows:totalRows]; 

    [cell configureData:myRowData]; 
} 

- (void)styleCellBackground:(BaseCustomTableViewCell *)cell 
        indexPath:(NSIndexPath *)indexPath 
        totalRows:(NSInteger)totalRows 
{ 
    UIImage *backgroundImage = nil;  

    if (indexPath.row == 0) 
    { 
     // Top row of this section   
     backgroundImage = self.topRowImage; // ivar loaded during 'viewDidLoad' 
    } 
    else if (indexPath.row == totalRows - 1) 
    { 
     // Bottom row of this section   
     backgroundImage = self.bottomRowImage; 
    } 
    else { 
     // Middle row of this section   
     backgroundImage = self.middleRowImage; 
    } 

    [cell updateRowBackground:backgroundImage]; 
} 

@implementation CustomTableViewCell 

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) 
    {   
     // Create my text field 
     _textField = [[UITextField alloc] initWithFrame:CGRectZero];   
     _textField.backgroundColor   = [UIColor whiteColor];  

     self.backgroundColor    = [UIColor whiteColor];    
     self.contentView.backgroundColor = [UIColor whiteColor]; 

     self.backgroundView = [[UIImageView alloc] init];   

     // not sure which of these should be set to opaque to ensure to meet criteria #4 
     // 4. Make your UITableViewCell's layer opaque (same goes for the content view if you have one) 
     self.backgroundView.opaque = YES; 
     self.layer.opaque = YES; 
     self.opaque = YES;   
     self.contentView.opaque = YES;   

     [self.contentView addSubview:_textField]; 
    } 
} 

- (void)layoutSubviews 
{  
    CGRect textFieldFrame = CGRectMake(10, 2, 278, 40); 
    self.textField.frame = textFieldFrame; 

    [super layoutSubviews]; 
} 

- (void)updateRowBackground:(UIImage *)rowBackground 
{ 
    ((UIImageView *)self.backgroundView).image = rowBackground;   
} 
+0

혼란 스럽지만 모든 셀마다 배경이 다른가요? 얼마나 많은 세포가 있습니까? 각기 다른 배경을 가진 셀이 많은 경우에는 동적으로로드해야합니다. viewdidload에서 모든 것을로드하면 인터페이스가 모든 준비가 완료 될 때까지 대기하게됩니다. 또한 디버깅을하고 많은 셀을 가지고 있다면 로딩 시간을 많이 늘리는 NSLOG를 사용하지 마십시오. – Pochi

+0

3 가지 배경 유형이 있습니다.각 섹션의 상단 행 셀은 1 유형 (아래로 곡선이 있음), 가운데 행은 다른 표준 이미지 (표준 이미지) 및 마지막으로 각 섹션의 맨 아래 행에 세 번째 유형 (이 이미지에는 위쪽 곡선이 있음)이 있어야합니다. –

+0

총 15 개의 행이 있으며 4 개의 섹션으로 나뉩니다. –

답변

0

:

여기 내 코드입니다.

3 가지 종류의 세포 ("상단", "중간"및 "하단")가있는 것 같습니다. 당신은 그것들을 교환 할 수 없게 재사용하는 것을 멈출 수 있습니다. 따라서 매번 사용할 때마다 배경 이미지를 재설정해야합니다.

대신 테이블의 위치에 따라 다른 식별자를 사용하여 표 셀의 인스턴스를 초기화 할 수 있습니다. 그렇게하면 "상단"식별자가있는 단일 셀, "하단"식별자가있는 단일 셀, "중간"식별자가있는 셀이 많이 생성됩니다. 그런 다음 셀을 초기화 할 때만 배경 이미지를 설정할 수 있습니다. 적절한 식별자로 셀을 재사용 할 때마다 더 이상 배경 이미지를 변경할 필요가 없습니다.

+0

나는 2 개의 다른 세포 유형이 있기 때문에, 나는 6 세포 변이가있을 것입니다. 나는 그 많은 다른 세포 유형을 갖는 것이 성능면에서 좋지 않다는 인상을 받았다. CocoaWithLove 사이트에서 사용할 수있는 코드 샘플에서 cellPath 유형은 1 개이지만 indexPath.row를 기반으로하는 다른 배경을 보았습니다. –

+0

더 많은 셀 식별자를 사용하면 메모리에있는 셀의 인스턴스를 더 많이 유지하는 테이블 뷰가 생성되지만 반드시 성능에 영향을 미치지는 않습니다. 나는 CocoaWithLove에 대한 Matt의 예가 셀 출현을 커스터마이징하는 방법을 보여 주며, 반드시 고성능 셀 생성에 대한 가이드가 아니라는 인상을 받고 있습니다. 지금 실제 성능 문제가있는 것 같아서 이러한 배경 이미지를 업데이트하는 데 드는 비용 때문에 문제가 발생했습니다. 문제를 제거하려고 시도하는 것이 합리적인 실험이라고 생각합니다. – Jonah

+0

그냥 이해하고 싶습니다. 상단, 중간 및 하단 행 배경을 사용자 정의 할 때 각 셀 유형에 대해 3 개의 다른 셀 식별자를 사용하여 큐에서 대기열에 추가합니다. 셀에 이질적인 데이터 또는 UI 요소가있는 경우 각 셀 유형마다 고유 한 셀 ID 유형이 추가됩니다. –