2015-01-04 77 views
0

背景如何在UITableView中设置Section属性?

我想设置部分在UITabelLabelCell属性。

原贴过时的OBJ-C代码/方法下面

这是代码来设置用于UITable内节标题的字体。我正在尝试为iOS7 +采用此代码。

的OBJ-C代码

[[UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil] setFont:[UIFont fontWithName:@"MyFont" size:8]]; 

我认为它应该是这个样子(并非一定):

UILabel.appearanceForTraitCollection(trait: UITraitCollection) 

,但我无法抄写UITraitCollection

+0

可能重复[appearanceWhenContainedIn在Swift](http://stackoverflow.com/questions/24136874/appearancewhencontainedin-in-swift) – akashivskyy 2015-01-04 14:54:06

回答

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... 
    } 

原帖下方(过时哈克溶液)

调查

在这个问题上搜索少量的Apple文档和SO Q & A,我无法弄清楚如何使用UITraitCollection

但是,我似乎发现了一个可能的破解this post in SO

我用下面的实现测试了它,它似乎工作。

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

加入桥接报

#import "BridgeAppearance.h" 

创建BridgeAppearance.h

@interface BridgeAppearance : NSObject { } 

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

@end 

创建BridgeAppearance.m

​​