参考:
[1]http://stackoverflow.com/questions/5869344/uitableview-header-footer-font-color
UITableview可以由多个Section组成,每个section可以由若干个row,并且可以为section设置title标题,可以用来设置标题。
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section; // fixed font style. use custom view (UILabel) if you want something different
这是个UITableViewDataSource的回调函数,当显示完成后,除非你roloadData,而roloadData会带来性能花费, 否则不会重新调用。因此,你也不能对实时的改变title。
这时可以通过返回一个view给Section head,这样你就可以addsubview显示任何你想显示的,并且将此view做为实例成员,就可以做到实时改变,而不需要reloadData。
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section; // custom view for header. will be adjusted to default or specified header height
但有个问题,你如何使显示的section 标题,跟通过titleForHeaderInSection回调显示的一样,就是你不清楚它所用的字体、颜色、等等信息。所以要想显示的跟系统一样就需要以下信息
Plain
fontName: Helvetica-Bold
pointSize: 18.000000
textColor: UIDeviceWhiteColorSpace 1 1
shadowColor: UIDeviceWhiteColorSpace 0 0.44
shadowOffset: CGSize 0 1
Grouped
fontName: Helvetica-Bold
pointSize: 17.000000
textColor: UIDeviceRGBColorSpace 0.298039 0.337255 0.423529 1
shadowColor: UIDeviceWhiteColorSpace 1 1
shadowOffset: CGSize 0 1
我用到的相关代码如下:
UILabel* sectionHeadLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, 320, 44)];
sectionHeadLabel.backgroundColor = [UIColor clearColor];
sectionHeadLabel.textColor = [UIColor colorWithRed:0.298039 green:0.337255 blue:0.423529 alpha:1.0];
sectionHeadLabel.shadowColor = [UIColor colorWithWhite:1.0 alpha:1.0];
sectionHeadLabel.shadowOffset = CGSizeMake(0, 1);
sectionHeadLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:17.0f];