2012-04-19 26 views

Trả lời

31

Bạn cần phải tạo cái nhìn tiêu đề của riêng bạn:

thực hiện trong nguồn dữ liệu tableview của bạn/ủy

- (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

Bạn chỉ có thể trả lại nhãn, không có lợi ích khi thêm nó dưới dạng chế độ xem phụ vào chế độ xem trống. – jrturton

+0

Có đúng .... nhưng có thể bạn muốn định vị lại nhãn của mình hoặc một cái gì đó như thế này ... –

+0

cảm ơn một tấn !!! Khi nhãn được trả về trực tiếp, nhãn KHÔNG được định vị ở vị trí X 6. Dựa trên sự hiểu biết của tôi, mà bao giờ xem được trả về, đó là 'frame.origin.x' và' frame.origin.y' bị bỏ qua. Vì vậy, khi nhãn được thêm vào dưới dạng chế độ xem con cho chế độ xem và chế độ xem được trả về, thì 'frame.origin.x' và' frame.origin.y' của nhãn được giữ nguyên bcuz, nó liên quan đến siêu xem của nó. – user1046037

14

có thể làm cho điều này quá:

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

.....

+1

+ 1 đây chính xác là những gì tôi đang tìm kiếm, cảm ơn – anneblue

+0

Nó sẽ không hoạt động trên iOS 5. – Dmitry

+0

Hoạt động nhờ hoàn hảo – jose920405

1

Tôi chỉ có thể xem tiêu đề thay đổi chiều cao cho tiêu đề cùng với chế độ xem

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ 
    return 110; 
} 
Các vấn đề liên quan