2014-02-05 37 views
0

我有包括3個部分(銷售代表,獎,簽名)自定義單元格 (見下圖)我如何生成多個tableView標題?

我想對我的tableView頭三個冠軍,但我只知道如何添加一個標題與內置方法。有人能幫我嗎?

enter image description here

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{ 
    return @"My Title"; 
{ 
+0

您可以得到該節號碼您必須返回第3節的否;在這個方法中「 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView」 – Pawan

回答

1

您需要創建自己的視圖來顯示爲節頭,而不是讓操作系統創建一個基於你想要的文字時一個給你。這使您可以完全自定義您想要包含在節標題中的內容。

您可以通過委託方法中返回一個視圖做到這一點:

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

如果你想每行拆分節頭,那麼你就需要有多個部分。每個部分都有1個部分標題,所以如果你想要3個標題,那麼你需要有3個部分。你可以告訴你的表要通過節數:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 

然後在委託方法,其提供了部分指標的整數,您使用的部分之間進行區分。如果他們提供了索引路徑,您可以通過indexPath.section

+0

它仍然只顯示一個標題。 – Greg

+0

你必須返回第3節的否;在這種方法「 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView」 – Pawan

+0

我試過這個,但我沒有看到一種方式來做多個頭這種方式。 – user3071579

0
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 
    UIView *view = nil; 
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 40)]; 
    CGFloat x=0; 
    for (int i = 0;i<[list count];i++) 
    { 
    id val=[list objectAtIndex:i]; 
    CGFloat val1=[val floatValue]; 
header.backgroundColor = [UIColor grayColor]; 

    UILabel *newLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, 0,107,40)]; 

    newLabel.text=[NSString stringWithFormat:@"%@",list[i]]; 

    newLabel.textAlignment = NSTextAlignmentCenter; 

    newLabel.backgroundColor = [UIColor greenColor]; 
    newLabel.layer.borderColor = [UIColor whiteColor].CGColor; 
    newLabel.layer.borderWidth = 2; 
    [header addSubview:newLabel]; 

    x=x+newLabel.frame.size.width; 


} 

return header; 
} 
+0

用於循環逐一添加到標頭中。根據標頭視圖大小設置表視圖大小..就是這樣。 –