2013-04-15 57 views
0

我需要幫助。我有三個UITableViews在一個UIView。不過,我只想要一個UITableView有一個標題。我怎樣才能在代碼中說出來?我已經有了以下代碼。多謝你們。只爲一個uitableview設置標題

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 
    if (tableView == Table1) { 
     return @"My Header Text"; 
    } else if (tableView == Table2) { 
     return nil; 
    } else if (tableView == Table3) { 
     return nil; 
    } 
} 
+0

PLZ(從我+1)發送您的UITableView代碼 –

回答

1

如果不工作,你也可以嘗試返回零爲頭的高度,以及:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { 
    if (tableView == Table1) 
    { 
     return 50.0; // or what's the height? 
    } 
    else 
    { 
     return 0.0; 
    } 
} 
+1

檢查R.A的答案,它beetter解決你的問題是這樣的。 –

+0

這對我有用:)) – Godric

+0

但是,如何使它保持在它的位置。,我的意思是,當我滾動時,它拖動.. – Godric

2

您所提供的代碼將添加頁眉視圖只有某部分。所以如果你有兩個或更多的部分,那麼你需要編寫額外的邏輯來爲每個部分設置標題視圖。

在這裏,它是一個簡單的方法來設置單個表的tableHeaderView是。

UILabel *label = [[UILabel alloc]initWithFrame:CGRectZero]; 
label.text = @"my table header"; 
table1.tableHeaderView = label; 
0

試試這個

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 
NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section]; 
if (sectionTitle == nil) { 
return nil; 
} 
UILabel *label = [[[UILabel alloc] init] autorelease]; 
label.frame = CGRectMake(20, 6, 300, 30); 
label.textColor = [UIColor blackColor]; 
label.font = [UIFont boldSystemFontOfSize:16]; 
label.text = sectionTitle; 

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, SectionHeaderHeight)]; 
    [view addSubview:label]; 
    return view; 
    } 
+0

這在iOS 5+中不起作用,就「在iOS 5.0之前,table view會自動調整header的高度爲0,以使tableView:viewForHeaderInSection返回一個零視圖。在iOS 5.0及更高版本中,您必須在此方法中返回每個節標題的實際高度。「 –

相關問題