我有一个表格视图,插入了常见的东西,问题出现在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];
}
以下是它最初的出现以及细胞重复使用后的情况。
答案 0 :(得分: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);
答案 1 :(得分:0)
显然,dequeueReusableHeaderFooterViewWithIdentifier:如果你在其他地方调用了registerClass:forHeaderFooterViewReuseIdentifier:方法(你做了),则总是返回一个视图。
我在上面的代码中看到的一个问题是,在从dequeue方法获取标题视图后,您调用自定义初始值设定项(即initWithQuestion :)。您不应该两次初始化对象。第一个初始化程序由dequeue方法隐式调用,即框架调用头视图的init方法。