【问题标题】:How to implement `viewForHeaderInSection` on iOS7 style?如何在 iOS7 样式上实现 `viewForHeaderInSection`?
【发布时间】:2013-09-20 00:03:17
【问题描述】:

如何在 iOS7 风格上实现(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 一样有效?

附:我只需要更改UITableView 标题的颜色并保存它的样式。

【问题讨论】:

    标签: objective-c iphone uitableview ipad ios7


    【解决方案1】:

    您可以在使用 willDisplayHeaderView 进行渲染之前更改标准 tableview 标题内的标签颜色。也适用于 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];
        }
    }
    

    供参考

    UITableViewHeaderFooterViewhttps://developer.apple.com/documentation/uikit/uitableviewheaderfooterview

    UITableViewDelegate 协议https://developer.apple.com/documentation/uikit/uitableviewdelegate/1614905-tableview

    【讨论】:

    • 太棒了!无需创建 UIView 即可更改颜色(以及字体和大小)的最简单方法。
    【解决方案2】:

    如果您需要更改页眉和页脚的全局颜色,请使用appearance

    [[UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil] setTextColor:[UIColor redColor]];
    

    【讨论】:

    • 整洁。没有想过这个。谢谢。
    【解决方案3】:

    在视图控制器生命周期的早期(例如,-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;
    

    这就是你使用默认实现的方式。

    【讨论】:

    • 艾萨克,谢谢!但我在 iOS 7 上只能看到这段代码的空白空间。出了什么问题?
    • 如果你说,headerFooterView.backgroundColor = [UIColor greenColor];你看到绿色的sectionView了吗?您可能还需要配置该 headerFooterView 的属性 - 首先在 Xcode 中的 viewForHeaderInSection 方法中设置断点,然后查看视图的框架是什么或者它是否为 nil...
    【解决方案4】:

    不完美但解决方案:

    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
        UILabel *label = [[UILabel alloc] init];
        label.textColor = [UIColor whiteColor];
        label.backgroundColor = [UIColor clearColor];
        if (<is_iOS7>) {
            label.text = [[NSString stringWithFormat:@"  %@", <title>] uppercaseString];
        } else {
            if (<is_iPad>) {
                label.text = [NSString stringWithFormat:@"          %@", <title>];
            } else {
                label.text = [NSString stringWithFormat:@"  %@", <title>];
            }
        }
        return label;
    }
    
    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
        return 38.f;
    }
    

    【讨论】:

      【解决方案5】:

      而@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])
          }
      }
      

      【讨论】:

        【解决方案6】:

        它的工作方式与 iOS 6 中的完全一样。创建一个视图(或只是一个覆盖整个标题的标签)并根据您的要求对其进行样式设置。

        【讨论】:

        • 我的要求是因为它默认工作。例如,在 iOS 7 上,它使用带有大写字符的纤细字体。该怎么做?
        猜你喜欢
        • 2014-04-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-27
        • 2016-08-31
        相关资源
        最近更新 更多