參考:
[1]http://stackoverflow.com/questions/5869344/uitableview-header-footer-font-color
[2]http://www.iphonedevsdk.com/forum/iphone-sdk-development/1199-default-tableview-section-header-background-color.html
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];