2011-05-31 66 views
6

我需要自定義UITableViewController的標題部分,其中爲每個部分返回不同的標題文本(也從數據源獲取數據)。這是通過以下方式完成的:爲UITableViewController自定義標題部分

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 
    NSArray *temp = [listOfMBeans allKeys]; 
    DLog(@"MBean details: %@", temp); 
    NSString *title = [temp objectAtIndex:section]; 
    DLog(@"Header Title: %@", title); 
    return title; 
}; 

這很好,我可以看到預期的輸出。不過,我也需要改變文本的字號,並尋找類似的問題後,我已經實現了以下內容:

- (UIView *) tableview:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 
    DLog(@"Custom Header Section Title being set"); 
    UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)] autorelease]; 

    UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)] autorelease]; 
    label.text = [tableView.dataSource tableView:tableView titleForHeaderInSection:section]; 
    label.backgroundColor = [UIColor clearColor]; 
    label.font = [UIFont boldSystemFontOfSize:14]; 

    [headerView addSubview:label]; 
    return headerView; 
} 

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

但是似乎代碼永遠不會調用。我的理解是,UITableViewController默認情況下將其設置爲委託,但似乎我錯了。

UITableViewController以這種方式創建(如分層數據的一部分):

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    ProjectDetails *detailViewController = [[ProjectDetails alloc] initWithStyle:UITableViewStyleGrouped]; 
    detailViewController.project = [listOfMetrics objectAtIndex:indexPath.row]; 

    // Push the detail view controller. 
    [[self navigationController] pushViewController:detailViewController animated:YES]; 
    [detailViewController release]; 
} 

什麼樣的變化,我應該做這方面的工作? 謝謝。

+0

它不是真的我清楚,那你有什麼要求? – rptwsthi 2011-05-31 13:51:59

+0

確保你設置了新的表格視圖委託,也許在'projectDetails'的init方法中。 – theiOSDude 2011-05-31 13:58:01

+0

@rptwsthi基本上如何使我的UITableViewController ProjectDetails調用viewForHeaderInSection以自定義標題視圖 – 2011-05-31 14:39:43

回答

3

您可以設置明確的委託:

detailViewController.tableView.delegate = detailViewController; 

或者你可以在控制器初始功能做到這一點。

編輯:你的init方法應符合規範初始化。此外,在我看來,你還沒有創建你的UITableView。嘗試並使用此代碼:

- (id)initWithStyle:(UITableViewStyle)style { 
    if ((self = [super initWithStyle:style])) { 
     self.tableView = [[[UITableView alloc] initWithFrame:self.view.bounds] autorelease]; 
     self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth UIViewAutoresizingFlexibleHeight; 
     self.tableView.delegate = self; 
    } 
    return self; 
} 

當然,你也可以做到這一切在筆尖文件...

+0

好的定義上,我在這一點上迷失了:(我已經嘗試了兩種解決方案,但沒有我已經添加了這個' - (id)initWithStyle:(UITableViewStyle)風格 {UITableViewController * tvc = [s uper initWithStyle:style]; tvc.tableView.delegate = self; return tvc; (UIView *)tableview:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section' – 2011-05-31 14:37:04

+0

請參閱我的編輯,請... – sergio 2011-05-31 14:55:41

+0

啊這就是訣竅......我需要創建我自己的UITableView並沒有使用默認生成的。非常感謝你! – 2011-05-31 15:09:27

2

你可以試試這個:在你的ProjectDetails.h中聲明一個UIView *tableHeader以及一個訪問器方法- (UIView *)tableHeader;。然後在實現文件:

- (UIView *)tableHeader { 
    if (tableHeader) 
     return tableHeader; 

    tableHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)]; 
    // addlabel 
    return tableHeader; 
} 

在viewDidLoad中,撥打電話:self.tableView.tableHeaderView = [self tableHeader];

我不相信你需要使用heightForHeaderInSection方法。

+0

嗨馬克,我已經試過這個,但雖然我可以看到該方法被稱爲節頭不變:(我仍然看到我的部分文本是從(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section' – 2011-05-31 14:31:28

+0

如果你想爲tableView中的多個部分創建自定義標題,那麼我可能誤解了你的問題。 – 2011-05-31 14:45:06

+0

是的,對於每個部分我有一個包含不同文本的頭文件(從數據源出來)我將編輯原始問題以清楚地說明這一點 – 2011-05-31 14:51:09

11

這個問題是一個較舊的一個,但我想分享我的代碼。我爲我的部分標題使用了常用的表格單元格視圖。我用接口生成器設計了它,並實現了下面的委託方法。

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: @"Header"]; 
    cell.textLabel.text = @"test"; 
    return cell; 
} 
+0

你爲什麼使用單元格作爲標題視圖?你是否使用兩種類型的單元格(標題/正文)作爲某種嵌套列表?我想知道sns應用程序是如何實現嵌套列表視圖的,以及如果你正在做類似的事情? – eugene 2012-08-19 13:48:23

+2

@Eugene,因爲你可以在IB中設計細胞。是的,我有嵌套列表和至少兩個單元格原型'header'和'item'。我不確定其他人是如何實施這種方法的。 – cocoafan 2012-08-21 07:57:32

+1

+1全能的智能和簡單的方法 – 2013-05-05 11:29:11

3

這裏是你如何讓準系統段查看了使用的UITableViewDelegate方法:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
    { 
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 40.0)]; 
    header.backgroundColor = [UIColor grayColor]; 

    UILabel *textLabel = [[UILabel alloc] initWithFrame:header.frame]; 
    textLabel.text = @"Your Section Title"; 
    textLabel.backgroundColor = [UIColor grayColor]; 
    textLabel.textColor = [UIColor whiteColor]; 

    [header addSubview:textLabel]; 

    return header; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 
    { 
    return 40.0; 
    } 
+0

由於您已將標籤框設置爲與聽者框相等,所以標題backgroundColor指令是無用的,因爲它被替換爲textLabel backgroundColor指令 – Max 2013-09-12 13:57:04

相關問題