2013-03-05 67 views
0

我正在嘗試創建標題標籤並縮進它,以便它出現在表視圖上方而不是刷新到屏幕左側。我已經實現了下面的代碼,它改變了標題標籤的外觀,但沒有改變位置。縮進UITableView標題標籤不工作

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

    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0,0,tableView.frame.size.width,30)]; 
    UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10,0, headerView.frame.size.width, 30)]; 

    headerLabel.backgroundColor = [UIColor clearColor]; 
    headerLabel.textColor = [UIColor whiteColor]; 
    headerLabel.shadowColor = [UIColor grayColor]; 
    headerLabel.shadowOffset = CGSizeMake(0.0, 1.0); 
    headerLabel.font = [UIFont boldSystemFontOfSize:18]; 
    headerLabel.text = @"Header Label"; 

    [headerView addSubview:headerLabel]; 
    return headerLabel; 
} 

任何人都可以看到我做錯了什麼?

感謝

+0

返回headerView而不是headerLabel,還有一件事,給頭部分的高度 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 35; }。你可以看到你的標題視圖。 – Nirmalsinh 2013-07-30 10:36:12

回答

2

回報烏爾headerView,不headerLabel。這將解決你的問題。

+0

這麼簡單!謝謝 – cullener 2013-03-05 23:02:14

+0

修改代碼: – sundsx 2013-04-17 15:53:08

0

我建議你修改這個代碼行:

UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0,0,tableView.frame.size.width,30)]; 
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10,0,  headerView.frame.size.width, 30)]; 

有:

UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0,0,100,30)]; 
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10,0, 100, 30)]; 

然後遞增或遞減的價值10填充左,0填充頂部...

希望幫助