如何在UITableView中设置Section属性?

kfm*_*e04 0 uikit swift

背景

我想在UITabelLabelCell中设置Section属性.

原始帖子与过时的Obj-C代码/方法下面

这是为UITable中的节头设置字体的代码.我正在尝试为iOS7 +采用此代码.

Obj-C代码

[[UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil] setFont:[UIFont fontWithName:@"MyFont" size:8]];
Run Code Online (Sandbox Code Playgroud)

我认为应该看起来像这样(不确定):

UILabel.appearanceForTraitCollection( trait: UITraitCollection )
Run Code Online (Sandbox Code Playgroud)

但我在抄写中遇到了麻烦UITraitCollection.

kfm*_*e04 5

编辑:iOS7 +的最佳当前解决方案

在UITableViewDelegate中实现tableView方法:

   func tableView(tableView: UITableView, willDisplayHeaderView view: UIView,
     forSection section: Int) {

        // Background color
        view.tintColor = UIColor.blackColor()

        // Text Color/Font
        let header = view as UITableViewHeaderFooterView
        header.textLabel.textColor = UIColor.greenColor()
        header.textLabel.font = my_font // put the font you want here...
    }
Run Code Online (Sandbox Code Playgroud)

下面的原始帖子(过时的hacky解决方案)

调查

搜索关于该主题和SO Q&A的少量Apple文档,我无法弄清楚如何使用UITraitCollection.

但是,我似乎在SO中找到了这篇文章的可能黑客.

我已经使用下面的实现测试了它,它似乎工作.

我们需要采取三个步骤来创建一个Objective-C类方法来完成这项工作.使用桥接头,我们可以从Swift无缝调用该方法.

添加到桥接标题

#import "BridgeAppearance.h"
Run Code Online (Sandbox Code Playgroud)

创建BridgeAppearance.h

@interface BridgeAppearance : NSObject { }

+ (void)setFontForUITableHeaderFooters: (UIFont*)font;

@end
Run Code Online (Sandbox Code Playgroud)

创建BridgeAppearance.m

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "BridgeAppearance.h"

@implementation BridgeAppearance

+ (void)setFontForUITableHeaderFooters: (UIFont*)font
{
    [[UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil]
     setFont: font];
}

@end
Run Code Online (Sandbox Code Playgroud)