2013-11-01 63 views
2

我有一個插入常見東西的表視圖,問題是在UITableViewHeaderFooterView,當他們第一次出現時,他們有不尋常的框架。但是,如果我向下滾動並備份(一旦它們被重用),它們顯得很好。這是相關的代碼。UITableViewHeaderFooterView繪圖不正確

編輯:另一方面表格視圖單元格顯示正常。

headerView的日誌在這一行 FAQSectionHeaderView * headerView = [self.tableView dequeueReusableHeaderFooterViewWithIdentifier:@「HeaderView」]; 示出了該:

2013-11-01 15:16:04.716 FAQDetail[27576:70b] header View <FAQSectionHeaderView: 0x8b582e0; baseClass = UITableViewHeaderFooterView; frame = (0 0; 0 0); text = ''; layer = <CALayer: 0x8b58420>> at Section 0 
2013-11-01 15:16:04.718 FAQDetail[27576:70b] header View <FAQSectionHeaderView: 0xa83edc0; baseClass = UITableViewHeaderFooterView; frame = (0 0; 0 0); text = ''; layer = <CALayer: 0xa83ee80>> at Section 1 
2013-11-01 15:16:04.719 FAQDetail[27576:70b] header View <FAQSectionHeaderView: 0x8c2f8e0; baseClass = UITableViewHeaderFooterView; frame = (0 0; 0 0); text = ''; layer = <CALayer: 0x8c2c370>> at Section 2 
2013-11-01 15:16:04.719 FAQDetail[27576:70b] header View <FAQSectionHeaderView: 0x8a3f7a0; baseClass = UITableViewHeaderFooterView; frame = (0 0; 0 0); text = ''; layer = <CALayer: 0x8a3f860>> at Section 3 
2013-11-01 15:16:04.720 FAQDetail[27576:70b] header View <FAQSectionHeaderView: 0x8c70dc0; baseClass = UITableViewHeaderFooterView; frame = (0 0; 0 0); text = ''; layer = <CALayer: 0x8c6f0b0>> at Section 4 
在ViewController.m

在FAQSectionHeaderView.m(UITableViewHeaderFooterView的子類)
- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
[self.tableView registerClass:[FAQSectionHeaderView class] forHeaderFooterViewReuseIdentifier:@"HeaderView"]; 
[self.tableView registerClass:[FAQDetailCell class] forCellReuseIdentifier:@"Cell"]; 
} 
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ 

FAQSectionHeaderView *headerView = [self.tableView dequeueReusableHeaderFooterViewWithIdentifier:@"HeaderView"]; 

[headerView setupWithQuestion:[self.sectionTitleArray objectAtIndex:section]]; 

return headerView; 

} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath  *)indexPath 
{ 
if (self.selectedSection == indexPath.section) { 

FAQDetailCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"Cell"]; 

[cell initWithAnswer:[self.rowsTitleArray objectAtIndex:indexPath.section]]; 

return cell; 
} else 
    return nil; 

} 

-(void)setupWithQuestion: (NSString *)question{ 
[self.questionLabel removeFromSuperview]; 

CGSize expectedLabelHeight = [FAQSectionHeaderView sizeForQuestion:question]; 

self.questionLabel = [[UILabel alloc] init]; 

self.questionLabel.frame = CGRectMake(10, 10, self.frame.size.width-20, expectedLabelHeight.height); 

NSMutableAttributedString *q = [[NSMutableAttributedString alloc] initWithString:[@"Q " stringByAppendingString:question]]; 

[q addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 2)]; 

[q addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(0, question.length)]; 


[q addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:17] range:NSMakeRange(0, 2)]; 

[self.questionLabel setAttributedText:q]; 

self.questionLabel.font = [UIFont systemFontOfSize:17]; 

self.questionLabel.numberOfLines = 0; 
[self addSubview:self.questionLabel]; 


self.frame = CGRectMake(0, 0, self.frame.size.width, self.questionLabel.frame.size.height+20); 

} 

-(void)prepareForReuse{ 

[self.questionLabel removeFromSuperview]; 

} 

這裏是如何最初和在後的細胞再利用踢出現。 enter image description hereenter image description here

回答

0

顯然,幀被設置爲(0,0,0,0),不像tableViewCell自動設置幀,

手動設置幀的技巧。

FAQSectionHeaderView *headerView = [self.tableView dequeueReusableHeaderFooterViewWithIdentifier:@"HeaderView"]; 

if (headerView == nil) { 
//This never gets called, even though it should according to documentation. 
    headerView = [[FAQSectionHeaderView alloc] init]; 
} 

headerView.frame = CGRectMake(0, 0, self.tableView.frame.size.width, [FAQSectionHeaderView sizeForQuestion:[self.sectionTitleArray objectAtIndex:section]].height+20); 
0

顯然,dequeueReusableHeaderFooterViewWithIdentifier:總是返回如果你調用的registerClass觀點:forHeaderFooterViewReuseIdentifier:方法在其他地方(你沒有)。

我在上面的代碼中看到的一個問題是,在從出列方法獲得標題視圖後,可以調用自定義初始化程序(即initWithQuestion :)。你不應該初始化一個對象兩次。第一個初始化器被dequeue方法隱式調用,即,框架調用了頭部視圖的init方法。

+0

感謝您查看它,但我沒有在initWithQuestion調用中初始化對象,它更像是一個setupWithQuestion :(方法實現在上面),我不在那裏做任何init活動。您是否面臨使用CGRectZero框架返回對象的問題? – ManicMonkOnMac