(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
作品のようなiOS7スタイルに実装する方法は(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
?
PSヘッダーのタイトルの色を変更してUITableView
スタイルを保存するだけです。
(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
作品のようなiOS7スタイルに実装する方法は(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
?
PSヘッダーのタイトルの色を変更してUITableView
スタイルを保存するだけです。
willDisplayHeaderView でレンダリングする前に、標準のテーブルビュー ヘッダー内のラベルの色を変更できます。また、iOS6/iOS7 の両方で動作します。
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
if([view isKindOfClass:[UITableViewHeaderFooterView class]]){
UITableViewHeaderFooterView *tableViewHeaderFooterView = (UITableViewHeaderFooterView *) view;
tableViewHeaderFooterView.textLabel.textColor = [UIColor blueColor];
}
}
参考のため
UITableViewHeaderFooterView : https://developer.apple.com/documentation/uikit/uitableviewheaderfooterview
UITableViewDelegate プロトコル: https://developer.apple.com/documentation/uikit/uitableviewdelegate/1614905-tableview
ヘッダーとフッターの両方のグローバル カラーを変更する必要がある場合は、次を使用しますappearance
。
[[UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil] setTextColor:[UIColor redColor]];
View Controller のライフサイクルの早い段階で (例: -viewDidLoad
)、クラスを登録します。
[[self tableView] registerClass:[UITableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:@"headerFooterReuseIdentifier"];
次に、メソッドで次のようにします。(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
セルを deque し、必要に応じてスタイルを設定して返します。
UIColor *titleColor = ... // Your color here.
UITableViewHeaderFooterView *headerFoorterView = [[self tableView] dequeueReusableHeaderFooterViewWithIdentifier:@"headerFooterReuseIdentifier"];
[[headerFooterView textLabel] setTextColor:titleColor];
return headerFooterView;
そして、それがデフォルトの実装を使用する方法です。
また、@aumansoftware の回答は、ボタンなどの標準ヘッダーにコンテンツを追加する最良の方法でもあります。Swift での例を次に示します。
override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
if (view.isKindOfClass(UITableViewHeaderFooterView)) {
let headerView = view as UITableViewHeaderFooterView
// Label
headerView.textLabel.textColor = UIColor.blueColor()
// New Button
let addPeopleButton: UIButton = UIButton.buttonWithType(.ContactAdd) as UIButton
addPeopleButton.addTarget(self, action: "showPeoplePicker:", forControlEvents: UIControlEvents.TouchUpInside)
addPeopleButton.setTranslatesAutoresizingMaskIntoConstraints(false)
headerView.addSubview(addPeopleButton)
let right: NSLayoutConstraint = NSLayoutConstraint(item: addPeopleButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: -12)
let vertical: NSLayoutConstraint = NSLayoutConstraint(item: addPeopleButton, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0)
headerView.addConstraints([right, vertical])
}
}