2011年6月30日木曜日

テーブルのセクションタイトルの文字色を変更する。

テーブル(UITableView)のセクションタイトルは、以下の記述ではデフォルトの文字色から変更することはできません。


-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
if (section == 0) {
return @"E-mail";
}
else if (section == 2){
return @"Sound";
}
else {
return @"Value Added Tax (VAT)";
}
}

そこで以下の記述とすると、セクションタイトルにビューを返すことができ、背景を黒とし、文字の色を白くすることができます。

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
if (section == 0) {
UIView *v = [[UIView alloc] init];
v.backgroundColor = [UIColor blackColor];
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 30.0f)];
UIImage *bgImage = [UIImage imageNamed:@"topTableSectionTitleBack.png"];
lbl.backgroundColor = [UIColor colorWithPatternImage: bgImage];
lbl.textColor = [UIColor whiteColor];
lbl.text = @" E-mail";
[v addSubview:lbl];
[lbl release];
return v;
}
else if (section == 2){
UIView *v = [[UIView alloc] init];
v.backgroundColor = [UIColor blackColor];
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 30.0f)];
UIImage *bgImage = [UIImage imageNamed:@"topTableSectionTitleBack.png"];
lbl.backgroundColor = [UIColor colorWithPatternImage: bgImage];
lbl.textColor = [UIColor whiteColor];
lbl.text = @" Sound";
[v addSubview:lbl];
[lbl release];
return v;
}
else {
UIView *v = [[UIView alloc] init];
v.backgroundColor = [UIColor blackColor];
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 30.0f)];
UIImage *bgImage = [UIImage imageNamed:@"topTableSectionTitleBack.png"];
lbl.backgroundColor = [UIColor colorWithPatternImage: bgImage];
lbl.textColor = [UIColor whiteColor];
lbl.text = @" Value Added Tax (VAT)";
[v addSubview:lbl];
[lbl release];
return v;
}
}

しかしながら、このままではセクションのテーブルがタイトル文字と被ってしまいます。そこでテーブルのプロパティにあるHedderのサイズを30とするならば、かぶりを解消することができます。気づくのに結構な時間を費やしてしまった。

0 件のコメント:

コメントを投稿