2012-01-12 80 views
4

我想改變tableHeader高度,字體等...heightForHeaderInSection沒有被調用?

我實現UITableViewDelegateUITableViewDataSource並添加heightForHeaderInSectionviewForHeaderInSection。但是這兩種方法沒有被調用。其他方法,如numberOfRowsInSection/cellForRowAtIndexPath工作正常。我可以看到表,但沒有頭:(

任何想法

這裏是代碼:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [self.poHeader.itemList count]; 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    NSLog(@"numberOfSectionsInTableView"); 
    return 1; 
} 

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


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

    NSLog(@"***in viewForHeader In section"); 

    UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(2, 166, 300.0, 44.0)]; 
    UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectZero]; 
    headerLabel.backgroundColor = [UIColor clearColor]; 
    headerLabel.opaque = NO; 
    headerLabel.textColor = [UIColor blackColor]; 
    headerLabel.highlightedTextColor = [UIColor whiteColor]; 
    headerLabel.font = [UIFont systemFontOfSize:13]; 
    headerLabel.frame = CGRectMake(2, 166, 300.0, 44.0); 

    headerLabel.text = @"Part No. Description Ext Cost"; // i.e. array element 
    [customView addSubview:headerLabel]; 

    return customView; 
} 
+0

即使調用委託方法,headerLabel也會出現在錯誤的地方(太低)。它的原點應該是0,0而不是2,166,因爲它是customView的子視圖。 – Anna 2012-01-12 23:10:12

回答

16

差不多一年後,但我猜你只設置數據源和?沒有委託

你必須有這樣的事情在你的控制器:

myTableview.delegate = self; 
myTableview.dataSource = self; 

您提到numberOfRowsInSection/cellForRowAtIndexPath正在被調用。兩者都屬於UITableViewDataSource而不屬於UITableViewDelegate

+0

差不多2年後,這是值得接受的檢查,因爲是正確的。 – giampaolo 2013-12-09 11:04:29

+1

這工作!這是正確的答案! – 4FunAndProfit 2013-12-15 01:48:28

+0

我不知道爲什麼,但即使表視圖作爲委託和數據源都連接到視圖控制器,但委託連接已斷開。無法追查,但這個答案給了我一個方法弄清楚。 – 2015-04-20 14:22:46