2011-09-12 8 views
6

個別のセクションを持つインデックス付きのUITableViewがあります。私は各セクションのヘッダービューに異なる背景色を使用したいと思います。私はちょうどその背景を変更する必要があり、標準ビューは正常に見える - (例えば、question # 2898361を参照)、それは私には「あまりにも多くの仕事」のようだ:viewForHeaderInSection:私は、私は完全にtableView実装することによって、自分自身のビューをロールバックすることができます知っています色。tableViewの標準のviewForHeaderInSectionにはどうやってアクセスできますか?

しかし、どのように私は、この標準のビューにアクセスできますか? [super tableView:viewForHeaderInSection:]を使用することはできません。これは、プロトコルの実装の問題であり、継承の問題ではないためです。私は標準的なビューを得ることができる他の方法ですか?

答えて

13

私はあなたがこれを簡単に行うことができませんほぼ確実です。私は最近、私の開発サポートアカウントの技術サポートリクエストの1つを使用して、UITableViewセクションの背景と境界の変更について最近質問しました。アップルのエンジニアは、これは本当に簡単なことではないと言いました。あなたがそれをやったとしても、パフォーマンスにはおそらく影響します。彼はまたcocoawithloveに私を指摘し、編集uitableviews記事:

http://cocoawithlove.com/2009/08/adding-shadow-effects-to-uitableview.html

本当に、あなた自身のヘッダを作成すると、その多くの努力ではありません。それがコメントアウトされたので、すぐに動作しない可能性があります - - 以下は、私は私のプロジェクトの1から引き出されたいくつかのコードですが、あなたのアイデアを得ることができます。

- (CAGradientLayer *) greyGradient { 
    CAGradientLayer *gradient = [CAGradientLayer layer]; 
    gradient.startPoint = CGPointMake(0.5, 0.0); 
    gradient.endPoint = CGPointMake(0.5, 1.0); 

    UIColor *color1 = [UIColor colorWithRed:255.0f/255.0f green:255.0f/255.0f blue:255.0f/255.0f alpha:1.0]; 
    UIColor *color2 = [UIColor colorWithRed:240.0f/255.0f green:240.0f/255.0f blue:240.0f/255.0f alpha:1.0]; 

    [gradient setColors:[NSArray arrayWithObjects:(id)color1.CGColor, (id)color2.CGColor, nil]]; 
    return gradient; 
} 

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 
    CGFloat width = CGRectGetWidth(tableView.bounds); 
    CGFloat height = [self tableView:tableView heightForHeaderInSection:section]; 
    UIView *container = [[[UIView alloc] initWithFrame:CGRectMake(0,0,width,height)] autorelease]; 
    container.layer.borderColor = [UIColor grayColor].CGColor; 
    container.layer.borderWidth = 1.0f; 
    CAGradientLayer *gradient = [self greyGradient]; 
    gradient.frame = container.bounds; 
    [container.layer addSublayer:gradient]; 

    UILabel *headerLabel = [[[UILabel alloc] initWithFrame:CGRectMake(12,0,width,height)] autorelease]; 
    headerLabel.backgroundColor = [UIColor clearColor]; 
    headerLabel.font= [UIFont boldSystemFontOfSize:19.0f]; 
    headerLabel.shadowOffset = CGSizeMake(1, 1); 
    headerLabel.textColor = [UIColor whiteColor]; 
    headerLabel.shadowColor = [UIColor darkGrayColor]; 
    NSString *title = [self tableView:tableView titleForHeaderInSection:section]; 
    headerLabel.text = title; 
    return container; 
} 

#import <QuartzCore/QuartzCore.h> 

によって確認してください方法は...これは標準ヘッダーの外観を模倣するものではありません - その単なる例です。しかし、私は少し試行錯誤して、標準のものを模倣して色を少し変えることができると確信しています。

+1

テキストに何かが見つからないようです。#import what?おそらくいくつかのコアアニメーションのものでしょうか? – Thorsten

+0

ああ、CAGradientLayerを使用したのはQuartzCoreでした。ポストに追加します。ありがとう – bandejapaisa

+0

また、[container addSubview:headerLabel]が見つからない場合は、このようなことをすれば本当に良いでしょう。 \t CGFloat width = tableView.bounds.size.width; \t CGFloatの高さ= [self tableView:tableView heightForHeaderInSection:section]; UIView * container = [[UIView alloc] initWithFrame:CGRectMake(0,0、width、height)]; –

1

@bandejapalsaソリューションの一つの問題があります:前のセルのセパレータは、それがデフォルトのiOS sectionHeaderView上にないとして、この実装ではまだ表示されています。私が見つけた解決策は、CALayerを使用して1ピクセル分オフセットすることでした。イメージは、ビューフレーム自体より1pix大きい必要があります。

// Create the view for the header 
CGRect aFrame =CGRectMake(0, 0, tableView.contentSize.width, IMIUICustomisation.sectionHeaderViewHeight); 
UIView * aView = [[UIView alloc] initWithFrame:aFrame]; 
aView.backgroundColor = UIColor.clearColor; 

// Create a stretchable image for the background that emulates the default gradient, only in green 
UIImage *viewBackgroundImage = [[UIImage imageNamed:@"greenheader.png"] stretchableImageWithLeftCapWidth:12 topCapHeight:0]; 

// Cannot set this image directly as the background of the cell because 
// the background needs to be offset by 1pix at the top to cover the previous cell border (Alex Deplov's requirement ^_^) 
CALayer *backgroungLayer = [CALayer layer]; 

backgroungLayer.frame = CGRectMake(0, -1, tableView.contentSize.width, IMIUICustomisation.sectionHeaderViewHeight+1); 
backgroungLayer.contents = (id) viewBackgroundImage.CGImage; 
backgroungLayer.masksToBounds = NO; 
backgroungLayer.opacity = 0.9; 
[aView.layer addSublayer:backgroungLayer]; 

// Take care of the section title now 
UILabel *aTitle = [[UILabel alloc] initWithFrame: CGRectMake(10, 0, aView.bounds.size.width-10, aView.bounds.size.height)]; 
aTitle.text = [delegate tableView:tableView titleForHeaderInSection:section]; 
aTitle.backgroundColor = UIColor.clearColor; 
aTitle.font = [UIFont boldSystemFontOfSize:18]; 
aTitle.textColor = UIColor.whiteColor; 

// Text shadow 
aTitle.layer.shadowOffset = CGSizeMake(0, 1); 
aTitle.layer.shadowRadius = .2; 
aTitle.layer.masksToBounds = NO; 
aTitle.layer.shadowOpacity = 0.5; 
aTitle.layer.shadowColor = IMIUICustomisation.selectedElementTextShadowColor.CGColor; 
[aView addSubview:aTitle]; 

return aView; 
5

他の回答を正しく使用すると、特定のセクションヘッダーのためにカスタマイズすることは何もない場合、あなたはtableView:viewForHeaderInSection:からnilを返し、テーブルビューの意志することができ、あなたはそれに簡単な変更を行うために、デフォルトのビューにアクセスすることはできません指摘するが、デフォルトのビューを使用します。あなただけのヘッダーの一部をカスタマイズする必要がある場合

これは便利です。

は、何らかの理由で、これはundocumentedです。

関連する問題