我有一个插入常见东西的表视图,问题是在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];
}
这里是如何最初和在后的细胞再利用踢出现。
感谢您查看它,但我没有在initWithQuestion调用中初始化对象,它更像是一个setupWithQuestion :(方法实现在上面),我不在那里做任何init活动。您是否面临使用CGRectZero框架返回对象的问题? – ManicMonkOnMac