2011-10-05 28 views
2

一個的UITableViewDelegate方法,我想從我的視圖控制器類調用下面的方法:撥打國際長途的ViewController

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section; 

但是我的生活中,我無法弄清楚如何做到這一點。我試過:

所有給我的錯誤。這是多餘的tableView:位。任何人都可以提供一些建議或至少解釋tableView:(UITableView *)tableView是什麼意思?

謝謝! Steve

回答

0

爲什麼要打電話給它?這是UITableViewDelegate方法之一,通常在tableView構造表時自動調用它。 當調用此方法時,tableView類對象會填充它需要的參數。視圖控制器只需提供由您自定義的正確的委託方法,以便它可以正確設置它。

您是否在代理類中自定義代碼(如本例中的代碼)?

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 
    // create the parent view that will hold header Label 
    UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(10.0, 0.0, 300.0, 44.0)]; 

    // create the button object 
    UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectZero]; 
    headerLabel.backgroundColor = [UIColor clearColor]; 
    headerLabel.opaque = NO; 
    headerLabel.textColor = [UIColor blackColor]; 
    headerLabel.highlightedTextColor = [UIColor whiteColor]; 
    headerLabel.font = [UIFont boldSystemFontOfSize:20]; 
    headerLabel.frame = CGRectMake(10.0, 0.0, 300.0, 44.0); 

    // If you want to align the header text as centered 
    // headerLabel.frame = CGRectMake(150.0, 0.0, 300.0, 44.0); 

    headerLabel.text = <Put here whatever you want to display> // i.e. array element 
    [customView addSubview:headerLabel]; 

    return customView; 
} 

我不在我的Mac附近,或者我會給你一個我自己的例子。但這是使用這種方法的一般方法。


順便說一句,你可以看到參數的tableView沒有示例代碼引用上方。如果您確實想調用此方法,請使用nil。 UITableViewDelegate協議允許控制器爲多個tableView進行委託。如果發生這種情況,該方法應測試以查看哪個tableView被引用,以便爲每個tableView調整專門的行爲。


附加信息:

如果你只是想看看你的tableView的標題的高度是什麼,你可以評估其sectionHeaderHeight財產。還有像這樣的其他屬性,如sectionFooterHeight和rowHeight。

您應該知道委託方法可以幫助您使用自定義的tableView。所以委託方法tableView:heightForHeaderInSection:實際上是爲了自定義標題高度。你的委託方法告訴tableView高度是多少。這不是檢查tableView屬性的方法。

蘋果文檔說,如果您使用tableView:heightForHeaderInSection進行自定義:那麼tableView sectionHeaderHeight無效。你會期望這一點,因爲該屬性指的是所有節標題的高度。

使用sectionHeaderHeight屬性,您可以在這種情況下寫入,您可以將所有標題設置爲相同的高度。

下面是我正在處理的一些示例代碼。我爲你添加了一條NSLog語句。

resultsTableVC = [[[ResultsTableVC alloc] initWithController:self] retain]; 

self.tableView = [[[UITableView alloc] initWithFrame:CGRectMake(0, 110, self.view.frame.size.width, self.view.frame.size.height-120) style:UITableViewStyleGrouped] retain]; 
self.tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight; 
self.tableView.autoresizesSubviews = YES; 
self.tableView.layer.cornerRadius = 10.0f; 

self.tableView.delegate = resultsTableVC; 
self.tableView.dataSource = resultsTableVC; 
self.tableView.backgroundView = nil; 
self.tableView.backgroundColor = [UIColor clearColor]; 
self.tableView.separatorColor = [UIColor defaultResultTableBackgroundColor]; 
self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine; 

[self.view addSubview:self.tableView]; 

NSLog(@"header: %f, row: %f", tableView.sectionHeaderHeight, tableView.rowHeight); 

(有人可能會指出,我不需要他們有些保留。我還在就這一工作。)

這告訴我標準部分的高度是100,標準的行高是44.0。我有一個指向我的tableView的指針,我可以通過這個類使用一個屬性。

現在,如果您使用tableView:heightForHeaderInSection設置標題高度,那麼您應該已經在程序中計算了高度。一旦設置好後,我認爲您不會查詢特定部分(或行)的高度。

這有幫助嗎?

+0

感謝您的回覆!它必須與heightForHeaderInSection相關。該方法的默認實現不起作用,而不是重複我的邏輯流程,我想獲得viewForHeaderInSection的結果,然後根據框架計算高度(如果有的話)。合理? –

+0

調用它的另一個原因是用於測試目的。 –

6

我不知道爲什麼你會怎麼稱呼它,但如果它是你是從調用它同一個對象實現,那麼你可以使用self

UIView* view = [self tableView:tableView viewForHeaderInSection:section]; 

否則,你可以從tableView中獲得委託並致電委託:

id<UITableViewDelegate> theDelegate = tableView.delegate; 
UIView* view = [theDelegate tableView:tableView viewForHeaderInSection:section]; 
相關問題