2014-09-02 33 views
0

我是iOS編程新手,我有一個要求,我需要爲表格顯示自定義標題部分。它應該類似於iOS:我如何爲表製作自定義標題部分?

enter image description here

我查了一下API /方法負責顯示標題,並發現

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 
    return @"HEADER"; 
} 

問題

  • 我需要建立一個單獨UITableViewCell?然後如何與標題進行縫合?

請推薦

謝謝

回答

0

你不必建立一個單獨的UITableViewCell。 如果您需要自定義tableView標頭,請構建您的自定義UIView,然後將其設置爲tableView的標題。

self.tableView.tableHeaderView = customView; 

或者,如果你需要的tableView的節頭設置爲自定義視圖:

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 
    UIView *sectionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 18)]; 
    //Add UILabels to sectionView, style it, etc. 

    return sectionView; 
} 
0

你可以這樣做:

tableView:viewForHeaderInSection: 

如果返回值是一個UIView

tableView:heightForHeaderInSection: 

返回值爲一個CGFloat的

或者,您可以設置:

[self.tableView setTableHeaderView:viewForHeader]; 

哪裏viewForHeader是您的自定義視圖。

注意:它們的行爲都不相同。如果您使用HeaderInSection,則標題將在該部分內滾動,直至與下一個標題衝突。

如果你設置了TableHeaderView,它將永遠停靠在你的tableView的頂部,它將取決於你想完成什麼。