2012-04-19 30 views

回答

31

您需要創建自己的頭看法:

你的tableview數據源中實現/委託

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 
    NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section]; 
    if (sectionTitle == nil) { 
     return nil; 
    } 

    // Create label with section title 
    UILabel *label = [[[UILabel alloc] init] autorelease]; 
    label.frame = CGRectMake(20, 6, 300, 30); 
    label.backgroundColor = [UIColor clearColor]; 
    label.textColor = [UIColor colorWithHue:(136.0/360.0) // Slightly bluish green 
           saturation:1.0 
           brightness:0.60 
             alpha:1.0]; 
    label.shadowColor = [UIColor whiteColor]; 
    label.shadowOffset = CGSizeMake(0.0, 1.0); 
    label.font = [UIFont boldSystemFontOfSize:16]; 
    label.text = sectionTitle; 

    // Create header view and add label as a subview 

    // you could also just return the label (instead of making a new view and adding the label as subview. With the view you have more flexibility to make a background color or different paddings 
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, SectionHeaderHeight)]; 
    [view autorelease]; 
    [view addSubview:label]; 

    return view; 
} 
+0

您可以只返回標籤,將其作爲子視圖添加到其他空視圖沒有任何好處。 – jrturton 2012-04-19 16:58:05

+0

是的正確....但也許你想重新定位你的標籤或類似的東西... – 2012-04-19 17:07:01

+0

謝謝噸!!!當標籤直接返回時,標籤不在X位置6.基於我的理解,返回哪個視圖,它是'frame.origin.x'和'frame.origin.y'被忽略。因此,當標籤作爲子視圖添加到視圖並返回視圖時,標籤的「frame.origin.x」和「frame.origin.y」將被保留,與它的超級視圖相關。 – user1046037 2012-04-20 03:05:22

14

可以做出這樣太:

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section 
{ 
    [[((UITableViewHeaderFooterView*) view) textLabel] setTextColor:[UIColor whiteColor]]; 
} 

.....

+1

+ 1這正是我正在尋找的,謝謝 – anneblue 2013-06-24 14:53:54

+0

它不適用於iOS 5. – Dmitry 2013-09-30 16:55:06

+0

作品完美謝謝 – jose920405 2016-02-25 21:37:07

1

我能夠僅查看標題爲標題添加高度以及視圖

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ 
    return 110; 
}