我有一个插入了常用东西的表格视图,问题出在 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
- (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;
}
在 FAQSectionHeaderView.m(UITableViewHeaderFooterView 的子类)
-(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];
}
这是它最初和单元重用开始后的显示方式。