[英]UITableView section header is all black
對於iPhone,我有一個UITableView被分組,有一個部分,我在其中設置了一個節標題,它是來自筆尖的UILabel對象。 顯示表格視圖時,標題顯示為純黑色條紋 - 無文本。
在heightForHeaderInSection中,我將高度設置為UILabel對象的frame.size.height。 當我在IB中改變高度時,黑條紋的高度會發生變化。 所以我知道.m文件已經鎖定到正確的UILabel對象。
在調試器中,在viewForHeaderInSection中,似乎UILabel對象的寬度為零,高度為1079574528,文本為null。
對我做錯了什么的想法?
不確定你做錯了什么,但這里有一些可能有用的示例代碼(來自我博客上的帖子 ):
#define SectionHeaderHeight 40
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
if ([self tableView:tableView titleForHeaderInSection:section] != nil) {
return SectionHeaderHeight;
}
else {
// If no section header title, no section header needed
return 0;
}
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
if (sectionTitle == nil) {
return nil;
}
// Create label with section title
UILabel *label = [[[UILabel alloc] init] autorelease];
label.frame = CGRectMake(20, 6, 300, 30);
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor colorWithHue:(136.0/360.0) // Slightly bluish green
saturation:1.0
brightness:0.60
alpha:1.0];
label.shadowColor = [UIColor whiteColor];
label.shadowOffset = CGSizeMake(0.0, 1.0);
label.font = [UIFont boldSystemFontOfSize:16];
label.text = sectionTitle;
// Create header view and add label as a subview
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, SectionHeaderHeight)];
[view autorelease];
[view addSubview:label];
return view;
}
我有同樣的問題,並沒有完全弄清楚為什么黑色吧..
但是,如果我為tableView.tableHeaderView
和tableView.tableFooterView
設置值,而不是在委托方法中提供頁眉和頁腳視圖, tableView.tableFooterView
沒事了!
加載數據源后刷新時觀察到相同的行為。 我注意到這是由於我刷新表視圖的方式。
//[self loadView]; this caused the section header to go black.
[self.tableView reloadData]; // this works!
3.1.3不喜歡[UIColor clearColor]; 嘗試使用與tableview相同的背景顏色
你可以發布你的heightForHeaderInSection和你的viewForHeaderInSection函數的代碼嗎? 你正在做的事情背后的理論聽起來是正確的,但是如果沒有看到代碼,幾乎不可能找出問題......
聽起來你在IB的視圖上放置了一個標簽,並試圖將其用作標題視圖 - 這不是正確的做事方式。 如果你沒有使用viewForHeaderInSection ,那就嘗試一下..像這樣:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UILabel *lbl;
lbl.text = @"Header for The Only Section";
//define other properties for the label - font, shadow, highlight, etc...
return lbl;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.