22

(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section作品のようなiOS7スタイルに実装する方法は(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section?

PSヘッダーのタイトルの色を変更してUITableViewスタイルを保存するだけです。

4

6 に答える 6

52

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

于 2013-09-22T22:12:55.773 に答える
8

ヘッダーとフッターの両方のグローバル カラーを変更する必要がある場合は、次を使用しますappearance

[[UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil] setTextColor:[UIColor redColor]];
于 2013-10-24T15:45:22.770 に答える
6

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;

そして、それがデフォルトの実装を使用する方法です。

于 2013-09-15T22:07:18.963 に答える
1

また、@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])
    }
}
于 2014-09-28T13:47:59.703 に答える