2009-06-06 7 views
28

성능을 위해 일반적으로 UITableView '셀을 다시 사용합니다. TableView 헤더보기에서 동일한 작업을 수행 할 수있는 방법이 있습니까? 나는 대리인의 방법으로 반환되는 것들에 대해 이야기하고 예상대로 나는 작동하지 않는 것 다음을 수행하려고재사용 가능한 TableView 헤더보기

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 

은 :

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 
    static NSString *CellIdentifier = @"Header"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier]; 
    if (cell == nil) { 
     cell = [self getHeaderContentView: CellIdentifier]; 
    } 
    return cell; 
} 

방법에 있는가 헤더 '보기를 재사용 하시겠습니까?

+1

을 따를 수'reuseIdentifier을 사용합니다 UITableViewHeaderFooterView'. – h4xnoodle

+0

하지만 현재 UI Builder를 사용하여 UITableViewHeaderFooterView를 사용할 수 없습니다. –

답변

32

Apple이 tableview 셀을 재사용 할 수있는 이유는 tableview에 많은 행이있을 수 있지만 화면에 소수만 표시되기 때문입니다. 각 셀에 메모리를 할당하는 대신 응용 프로그램은 이미 존재하는 셀을 재사용하고 필요에 따라 재구성 할 수 있습니다.

먼저 헤더보기는 UIView 일 뿐이며 UITableViewCell은 UIView의 하위 클래스이지만 섹션 헤더의보기로 배치되지 않습니다.

또한 전체 행보다 섹션 헤더가 훨씬 적기 때문에 재사용 메커니즘을 구축 할 이유가 거의 없으며 사실 Apple은 일반 UIView를 구현하지 않았습니다.

레이블을 헤더로 설정하는 중이라면 -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section을 대신 사용할 수 있습니다. 같은 빨간색 텍스트 (또는 버튼, 이미지 등)을 가진 레이블로 뭔가 더 사용자 정의, 당신은 같은 것을 할 수 있습니다 들어

:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 
    UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0,0, 320, 44)] autorelease]; 
    UILabel *label = [[[UILabel alloc] initWithFrame:headerView.frame] autorelease]; 
    label.textColor = [UIColor redColor]; 
    label.text = [NSString stringWithFormat:@"Section %i", section]; 

    [headerView addSubview:label]; 
    return headerView; 
} 
+0

레이블은 autorelease가 아니어야하며 headerView에 추가 된 후에 릴리스해야한다고 생각합니다. 너 마음이 뭐니? – gsempe

+14

"재사용 메커니즘을 구축 할 이유가 거의 없습니다 ..."Martins 코멘트는 일반적으로 정확하지만 완전히는 아닙니다 .60fps (나 같은)에 도달하려는 사용자의 경우 viewForHeaderInSection은 사용자 정의보기를 사용하여 악마입니다. 특히 각 그룹에 대한 새로운보기를 삽입 할 때 특히 그룹이 크지 않은 경우 , 실제로는 5FPS의 비용이 소요되므로 실제로는 성능에 도달 할 수 없게됩니다. 디자인에서 사용자 정의 헤더를 사용하지 않으면 성능이 향상됩니다. –

+2

하지만 실제로 사용자 정의 헤더보기를 사용해야하는 경우 , FPS를 60으로 유지하는 가장 좋은 방법은 무엇입니까? –

10

간단하면서도 효과적인 솔루션 :

@interface SectionedTableViewController() 
    @property (nonatomic, strong) UINib*   sectionHeaderNib; 
    @property (nonatomic, strong) NSMutableArray* sectionHeaders; 
@end 

@implementation SectionedTableViewController 

@synthesize sectionHeaderNib = sectionHeaderNib_; 
@synthesize sectionHeaders = sectionHeaders_; 

- (void) viewDidUnload 
{ 
    self.sectionHeaders = nil; 
    [super viewDidUnload]; 
} 

- (NSMutableArray*) sectionHeaders 
{ 
    if (!sectionHeaders_) 
     self.sectionHeaders = [NSMutableArray array]; 
    return sectionHeaders_; 
} 


- (UINib*) sectionHeaderNib 
{ 
    if (!sectionHeaderNib_) 
     self.sectionHeaderNib = [UINib nibWithNibName: NSStringFromClass(YourHeaderView.class) bundle: nil]; 
    return sectionHeaderNib_; 
} 


- (YourHeaderView*) dequeueHeader 
{ 
    return [self.sectionHeaders firstObjectPassingTest: ^(id obj) { return (BOOL) ([obj superview] == nil); }]; 
} 


- (NSString*) sectionHeaderTitleForSection: (NSInteger) section 
{ 
    return nil; 
} 


- (UIView*) tableView: (UITableView*) tableView viewForHeaderInSection: (NSInteger) section 
{ 
    YourHeaderView* headerView = [self dequeueHeader]; 
    if (!headerView) 
    { 
     headerView = [YourHeaderView instanceFromNib: self.sectionHeaderNib]; 
     [self.sectionHeaders addObject: headerView]; 
    } 
    return headerView; 
} 

@end 
+0

이것은 무엇입니까? firstObjectPassingTest? –

7

당신이 UIView의의 서브 클래스입니다 UITableViewHeaderFooterView 클래스 를 작성하여 그것을 구현할 수 당신은 또한 개별 주문을 작성해야 vidual XIB는 자동으로 생성되지 않으므로 UITableViewHeaderFooterView 생성. 있는 tableview와

등록 펜촉

[self.tblCart registerNib:[UINib nibWithNibName:@"CartHeaderView" bundle:nil] forHeaderFooterViewReuseIdentifier:@"CartHeader"]; 

이제 당신은 viewForHeaderInSection

-(UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 

    CartHeaderView *sectionHeader=[tableView dequeueReusableHeaderFooterViewWithIdentifier:@"CartHeader"]; 
return sectionHeader; 
} 

참고가 액세스 할 수 있습니다 는 섹션과 동일한 프레임 하위 뷰를 만들어야합니다 배경 색상을 설정하려면 헤더 및 해당보기의 색상을 설정합니다.

당신이 지금 가지고있는 아이폰 OS 6으로

Changing the background color on a UITableViewHeaderFooterView loaded from a xib says to use contentView.backgroundColor instead