私はiOS5を使用していて、テーブルに動的にセルを生成しました(それぞれ3行の2つのセクション)。各セクションには、titleForHeaderInSection
呼び出しを使用して動的に生成されるヘッダーがあります。
また、セクションヘッダーのデフォルトの色を読みにくくするために、テーブルの背景として画像を設定しています。セクションヘッダーの色を変更する方法が見つかりませんでした(またはshadow color
、font
、text size
など)ストーリーボードインターフェイスを介して、またはプログラムで!助けてください!
メソッドtableView:viewForHeaderInSection:
を使用してセクションヘッダーの独自のビューを作成することにより、フォントサイズ/色などを変更できます。
これはiOS5以降でも機能します。これは、テーブルビューのすべてのセクションヘッダーとフッターに適用され、私のニーズに適合しました。
- (void)viewDidLoad
{
[super viewDidLoad];
[[UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil] setFont:[UIFont fontWithName:@"MyFont" size:8]];
}
ダン
フォントや色を変更するなど、あまり変更を加えていない場合は、次のようにします。
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
UITableViewHeaderFooterView *tableViewHeaderFooterView = (UITableViewHeaderFooterView *)view;
tableViewHeaderFooterView.textLabel.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:12.0f];
tableViewHeaderFooterView.textLabel.textColor = [UIColor colorWithRed:0.27f green:0.27f blue:0.27f alpha:1.0f];
tableViewHeaderFooterView.contentView.backgroundColor = [UIColor colorWithRed:0.87f green:0.87f blue:0.87f alpha:1.0f];
}
UITableViewHeaderFooterView
クラスは、テーブルセクションの上部または下部に配置できる再利用可能なビューを実装します。ヘッダーとフッターを使用して、そのセクションの追加情報を表示します。
可用性:iOS(6.0以降)
例:
[[UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil] setColor:[UIColor whiteColor]];
に使用される手法と非常によく似ています
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
提供するセルプロトタイプのインスタンスを作成できます。セルにラベル用のコンセントが含まれている場合は、返却する前に設定できます。
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger) section
{
SessionTableViewCell *cell;
cell = [tableView dequeueReusableCellWithIdentifier:@"SessionSectionHeader"];
if (cell == nil) {
cell = [[SessionTableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:@"SessionSectionHeader"];
}
cell.myLabel.text = myTitles[section];
return cell;
}
@ "SessionSectionHeader"は、セルプロトタイプのストーリーボードの識別子であることに注意してください。
HTH!