2009-06-06 13 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

"再利用の仕組みを構築する理由はほとんどありません..."私のような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

あなたはUITableViewHeaderFooterViewクラス を作成することによって、それはあなたがまたINDIを作成する必要がのUIView のサブクラスであることを実装することができますXIBは自動的に作成されませんので、UITableViewHeaderFooterViewを作成してください。

テーブルビュー

[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; 
} 

になおアクセスすることができます:あなたはセクションと同じフレームでサブビューを作成する必要があります背景色を設定するには をそのビューのヘッダーと色を設定します。

あなたが今持っているのiOS 6のよう

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

関連する問題