2013-01-23 28 views
2

我已經有了一個UITableView分區,並且想要在頂部標籤的文本中設置一個標籤(而不是在tableview中)。從UITableView標題部分設置UILabel文本

我試圖設置標籤與[label setText:@"name"];

(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 

,但沒有奏效。任何人都有想法如何做到這一點?

+0

沒有ü實現什麼委託方法? – limon

+0

我想,我並不清楚。在我看來,我有一個約50x50的UILabel。我不希望tableHeaders,而只是設置該標籤上的文本,現在在每個標題部分設置一個靜態標題 – sjors

回答

2

如果您使用該方法,則只需要返回字符串。它會處理爲你創建一個標籤。

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

如果你想使用自己的UIViewUILabel你需要使用不同的方法dataSource

- (UIView *) tableview:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 
    UILabel *customLabel = [[UILabel alloc] initWithFrame:CGRect(0.0,0.0,100.0,20.0)]; 
    [customLabel setText:@"name"]; 

    return customLabel; 
} 
5

試試這個

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 60)]; 
    headerView.backgroundColor = [UIColor clearColor]; 

    UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake(0,0, 50, 50)]; 
    label.backgroundColor = [UIColor yellowColor]; 
    label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin; 

    [headerView addSubview:label]; 
    return headerView; 
} 
2
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 
    self.tableView1.tag = 1; 
    if ([tableView tag] == 1) { 
     return @"TableView One Title"; 
    } 
    return @"other Tableview Title";   
}