2009-08-28 12 views
29

私はtableViewCellをカスタマイズする方法を知っています。tableViewをカスタマイズする方法Section View - iPhone

多くのアプリケーションがtableView Cellをカスタマイズしています。 「 - セクション名が異なるフォントである必要があり、それが持つさまざまな背景画像などとし、」

は同様に、私は

テーブルビューセクションのヘッダーをカスタマイズしたいです

どうすればよいですか?

どのメソッドでコードを実装する必要がありますか?あなたはこの1つ実装したい

+2

+1に役立ちます。 :) – mAc

答えて

60

の代わりに通常の方法

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 

を使用して:あなたが見ることができるように

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

を、もう一つは代わりに、テキストのための単なる文字列のUIViewのを返します。したがって、独自のビュー(ラベルなど)をカスタマイズして返すことができます。ここで

あなたは(上記の方法で実装される)ことを行う可能性があります方法の例です:

// create the parent view that will hold header Label 
UIView* customView = [[[UIView alloc] initWithFrame:CGRectMake(10,0,300,60)] autorelease]; 

// create image object 
UIImage *myImage = [UIImage imageNamed:@"someimage.png"];; 

// create the label objects 
UILabel *headerLabel = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease]; 
headerLabel.backgroundColor = [UIColor clearColor]; 
headerLabel.font = [UIFont boldSystemFontOfSize:18]; 
headerLabel.frame = CGRectMake(70,18,200,20); 
headerLabel.text = @"Some Text"; 
headerLabel.textColor = [UIColor redColor]; 

UILabel *detailLabel = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease]; 
detailLabel.backgroundColor = [UIColor clearColor]; 
detailLabel.textColor = [UIColor darkGrayColor]; 
detailLabel.text = @"Some detail text"; 
detailLabel.font = [UIFont systemFontOfSize:12]; 
detailLabel.frame = CGRectMake(70,33,230,25); 

// create the imageView with the image in it 
UIImageView *imageView = [[[UIImageView alloc] initWithImage:myImage] autorelease]; 
imageView.frame = CGRectMake(10,10,50,50); 

[customView addSubview:imageView]; 
[customView addSubview:headerLabel]; 
[customView addSubview:detailLabel]; 

return customView; 

希望の素敵な質問のため

+1

たとえば、テーブルに2つのセクションがある場合、その2番目のテーブルセクションをどのように参照すると、セクションヘッダー1はセクションヘッダー2とは異なるheaderLabel.textを持つでしょうか? – iamtoc

+1

メソッドに渡される整数で指定します。 –

+2

section = 1で、section = 0でないヘッダーのみを使用する場合はどうすればよいですか?私はこの問題の問題を抱えています... – jsetting32

関連する問題